Saturday, May 22, 2010

How do you make rand more random in c programming?

i have a snake program in c and i want to make the food move randomly,there is 3 food moving at the same time one step at a time.I use a call function for the movement of the food.





int movement1()


{


return ((rand () % 4));


}





and another for each of the food.the problem is that all of them moves to the same direction and does'nt change the value for the every loop made.


*I used the function in the loop...

How do you make rand more random in c programming?
it is so becuase it has got same seed for all time use time(0) as seed and you will get fully random output from rand





for help see srand(int) and rand() in your programming manual
Reply:Simple example:





#include %26lt;iostream.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;





int main()


{


int random;


time_t seconds;


time(%26amp;seconds);





srand((unsigned int) seconds);





for(int i=0; i%26lt;100; i++){


random = rand() % 4;


cout%26lt;%26lt; random %26lt;%26lt; endl;


}


return 0;


}





This loops 100 times to give random integers between 0 and 3.





Rin this program several times and you will see different numbers are always generated.
Reply:NAME


rand, srand - random number generator.





SYNOPSIS


#include %26lt;stdlib.h%26gt;





int rand(void);





void srand(unsigned int seed);





DESCRIPTION


The rand() function returns a pseudo-random integer


between 0 and RAND_MAX.





The srand() function sets its argument as the seed for a


new sequence of pseudo-random integers to be returned by


rand(). These sequences are repeatable by calling srand()


with the same seed value.





If no seed value is provided, the rand() function is auto­


matically seeded with a value of 1.


--------------------------------------...


NAME


time - get time in seconds





SYNOPSIS


#include %26lt;time.h%26gt;





time_t time(time_t *t);





DESCRIPTION


time returns the time since the Epoch (00:00:00 UTC, Jan­


uary 1, 1970), measured in seconds.





If t is non-NULL, the return value is also stored in the


memory pointed to by t.





--------------------------------------...


NAME


ftime - return date and time





SYNOPSIS


#include %26lt;sys/timeb.h%26gt;





int ftime(struct timeb *tp);





DESCRIPTION


Return current date and time in tp, which is declared as


following:





struct timeb {


time_t time;


unsigned short millitm;


short timezone;


short dstflag;


};





The structure contains the time since the epoch in sec­


onds, up to 1000 milliseconds of more-precise interval,


the local time zone (measured in minutes of time westward


from Greenwich), and a flag that, if nonzero, indicates


that Daylight Saving time applies locally during the


appropriate part of the year.





--------------------------------------...


Call the function below before generating random numbers





#include %26lt;sys/timeb.h%26gt;





void initialize_random_numbers() /* call this first */


{


struct timeb tbuf;


ftime(%26amp;tbuf);


/* add milliseconds to make it more random */


srand(tbuf.time + tbuf.millitm);


}





then call your movement1() function.


No comments:

Post a Comment