s*******e 发帖数: 1269 | 1 I used
srand((unsigned) time(NULL));
for ( int i=0;i<5;i++)
{
rand();
}
I produce the same number at the same time period such as 5 second or 3 second
.
How can I produce a random number? | n******s 发帖数: 7 | 2 If you invoke this program repeatedly in a short period of time, you are going
to get the same random number sequence because you are calling srand() with
the same seed, namely the integer that is the number of seconds since midnight
1970/01/01. If you must repeatedly invoke the program, you will need to find
a way to hash up the seed value. You could try putting the program to sleep
for a second or two before calling srand().
second
【在 s*******e 的大作中提到】 : I used : srand((unsigned) time(NULL)); : for ( int i=0;i<5;i++) : { : rand(); : } : I produce the same number at the same time period such as 5 second or 3 second : . : How can I produce a random number?
| L**********r 发帖数: 83 | 3 But I test it, it generates different numbers?
second
【在 s*******e 的大作中提到】 : I used : srand((unsigned) time(NULL)); : for ( int i=0;i<5;i++) : { : rand(); : } : I produce the same number at the same time period such as 5 second or 3 second : . : How can I produce a random number?
|
|