[51] problem in Generating random nmber using rand() function in embedded c

Status
Not open for further replies.

MANO.5

Member level 1
Joined
Mar 15, 2013
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,618
i modified the example random number generation program from keil help section.
keil example program
Code:
#include <stdlib.h>
#include <stdio.h> /* for printf */

void tst_rand (void) {
  int i;
  int r;

  for (i = 0; i < 10; i++) {
    printf ("I = %d, RAND = %d\n", i, rand ());
  }
}

my program was
Code:
 #include<reg51.h>
 #include <stdlib.h>
 sfr lcd_data_pin=0xA0;//p2 port
sbit rs=P1^0;
sbit rw=P1^1;
sbit e=P1^2;
void delay(unsigned int count)    //Function to provide delay
{
    int i,j;
    for(i=0;i<count;i++)
    for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char value)
{
	lcd_data_pin = value;
	rs=0;
	rw=0;
	e=1;
	delay(1);
	e=0;
	delay(1);
}
void lcd_data(unsigned char value)
{
	lcd_data_pin =value;
	rs=1;
	rw=0;
	e=1;
	delay(1);
	e=0;
	delay(1);
}
void lcd_init()
{	
         lcd_cmd(0x38);
	lcd_cmd(0x0e);
	lcd_cmd(0x01);	
	lcd_cmd(0x80);
}
void lcd_string(unsigned char *disp)	 //LCD string function 
{
	char x;
	for(x=0;disp[x]!=0;x++)
	{
		lcd_data(disp[x]);
	}

}

  void lcd_data_int(int time_val)		//  to send three digit number
{
	int int_amt;
    int_amt = time_val/1000;
	lcd_data(int_amt+48);
	time_val = time_val%1000;
	int_amt = time_val/100;
	lcd_data(int_amt+48);
	time_val = time_val%100;
	int_amt = time_val/10;
	lcd_data(int_amt+48);
	int_amt = time_val%10;	
	lcd_data(int_amt+48);
}

void tst_rand (void) {
  unsigned int i;	  

  i= rand();
 lcd_cmd(0x80);
lcd_string("number: ");
delay(250);

lcd_cmd(0xc0);
 lcd_data_int(i);
 
 
}
void main()
{
lcd_init();
  while(1)
   {
   	  tst_rand ();
   }   

}

when i compile this code it gives 3 warnings and when simulate in proteus it was blinking...no output

this is the 3 warnings from keil

So help me to solve this issue
 

try
Code:
#include <stdlib.h>     /* srand, rand */

have you thought how you would seed the generator - it is only pseudo-random so unless you seed differently each time you run you will get the same sequence of numbers
 

Hello horace1,
ya it's pseudo-random .But i want to know what is this error & how to solve this
 


thank you horace1,
I am tried in keil also.But waiting for answers.. here also looking forward.pls anybody compile and post the result to know there is problem in my keil installation..
 

on different pc it was compiled without any error or warnings.But no output in lcd..so please somebody check is there any mistakes in program...........
 

as u are using rand function but whenever u feed same value it will return same output. so u have to feed different value as input so u will get really random numbers.
 

When i was simulating this program in proteus it didn't displaying anything...giving same number is not problem ..the problem is not displaying anything.....
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…