Hey, I noticed that when you loop a random number without a pause, it will output the same number like 20 times after eachother before changing the number. Example
5
5
5
5
5
5
5
5
2
2
2
2
2
Its not that I really need a work around for it, but I was wondering if there is any.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
usingnamespace std;
int main(){
while(true){
int roll;
srand (time(NULL));
roll = rand() % 6 + 1;
cout << "Number is "<< roll<<endl;
}
return 0;}
But you do get the fact that the sequence of numbers isn't actually random -- it's pretend (pseudo) random from a given starting point -- so if you set the same seed you will then get exactly the same sequence of random number for the following successive calls to rand (code below.)
0). seq for seed(123) = 41 54 76 5 64 66 50 79
1). seq for seed(123) = 41 54 76 5 64 66 50 79 - same as result set 0
2). seq for seed(123) = 41 54 76 5 64 66 50 79 - same as result set 0
3). seq for seed(42) = 76 1 70 57 84 80 17 45
4). seq for seed(180) = 27 21 26 58 71 81 51 65
5). seq for seed(180) = 27 21 26 58 71 81 51 65 - same as result set 4
6). seq for seed(42) = 76 1 70 57 84 80 17 45 - same as result set 3
7). seq for seed(123) = 41 54 76 5 64 66 50 79 - same as result set 0
The other part of the equation is that time() has a precision of seconds. So it will return the same value for up to a second (depending on when you make the first call.)
Texan's suggestion to call srand() with the value returned by (e.g.) the WinAPI QueryPerformanceCounter() function would work but is unnecessary work for the computer. I don't think would make the "random" numbers any more random. (If you're using a C++11 compliant compiler you could use one of the new mechanisms for number generation, but they are more involved to use. http://www.cplusplus.com/reference/random/ )
Moving srand() out of the loop, as bazetwo suggested, is the right solution (as a general rule you call srand() once at program startup.)
Andy
PS Turning it around, the fact that random numbers restart can be useful for testing purposes as you know the sequence will repeat. So when I use rand() in test code, I log the seed so I can feed it in if I need to repro a crash, for example. Sometimes the crash only occurs for just the right value!