Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Generatng random number in Keil C51 Compiler

Status
Not open for further replies.

Ahmad Bilal

Newbie level 2
Joined
Aug 22, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
20
I'm trying to generate random number in embedded C using "stdlib.h" and rand() function on Kiel C51 compiler. But every time i reset microcontroller, i get the same sequence of number not random numbers. I think its bcoz the 8051 family doesn't have a realtime timing clock. Any help regarding the problem will be appreciated....!!!

I want to generate a random number for luddoo simulation and display this random number on 7-Segment via Port 2:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
code is here:
/* Main.c file generated by New Project wizard
 *
 * Created:   Wed Jul 31 2013
 * Processor: AT89C51
 * Compiler:  Keil for 8051
 */
 
#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
 
unsigned int Random_Num();
void Delay_Loop();
 
void main(void)
 { 
     unsigned int random;
     P2=0;
     while(1)
         {
            random=(rand()%130);
            if(random==6){ 
                P2=random;
                Delay_Loop();
            }
            if(random==91){ 
                P2=random;
                Delay_Loop();
            }
            if(random==79){ 
                P2=random;
                Delay_Loop();
            }
            if(random==102){ 
                P2=random;
                Delay_Loop();
            }
            if(random==109){ 
                P2=random;
                Delay_Loop();
            }
            if(random==125){ 
                P2=random;
                Delay_Loop();
            }
         }
}
 
void Delay_Loop() 
{
    int d;
    for(d=0;d<=20;d++)
        {
            TMOD=0x01;
            TL0=0xFD;
            TH0=0x04B;
            TR0=1; 
            while(TF0==0); 
                TR0=0; 
            TF0=0;
            }
}

 

it's true. from keil documentation

The rand function generates a pseudo-random number between 0 and 32767

it's mean every time you restart your program it's generate the same sequence of numbers. You can use srand() function for each value it generate different sequence. Show this link

**broken link removed**

my idea is to make integer variable and save it in EEPROM and every time you start your program increase this value by one. Saving in EEPROM make srand(variable_saved_in_eeprom) function give different values in every time of start program
 
it's true. from keil documentation

The rand function generates a pseudo-random number between 0 and 32767

it's mean every time you restart your program it's generate the same sequence of numbers. You can use srand() function for each value it generate different sequence. Show this link

**broken link removed**

my idea is to make integer variable and save it in EEPROM and every time you start your program increase this value by one. Saving in EEPROM make srand(variable_saved_in_eeprom) function give different values in every time of start program


I'm New to 8051... Please if you can help me by providing the code of what you are telling me :) Thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top