Hey guys,
I am having a problem with the rand() function. The first time I execute the program, the random values come, but every time after that, the random values which are generated are the same. Here is my code :
The problem with srand( time (NULL)) is that the values returned are within touching distance of each other. For e.g., here are outputs when I ran the program four times
6201
6209
6212
6217
These values are not random enough. Another problem is that when I put in a for loop, the same values are returned each time.
Another problem is that when I put in a for loop, the same values are returned each time.
Note that seeding the random number generator with the same number will generate the same stream of pseudo-random numbers. Placing the call to srand in the loop like that reseeds it, but since it's been so short since the last call, likely time(NULL) is returning the same result as before, hence the same pseudo-random number. Try placing srand (time(NULL)); outside the loop (and call it only once).