-
Random number generator
I and my friend are having problems with random numbers.
If we use time(NULL) to seed the random number, the random number appears to be the same many times but chances after a while.
Ex. 49, 49, 49, 68, 68.
And it allways increments the number like this: 23, 33, 40, 54 and so one.
When it comes to the number that is inserted as the maximum value it starts over. Like this: 70, 89, 99, 4, 10 ...
We could use the GetTickCount() to seed the random number but the value increments then too and that is not pure random.
-
-
FAQ: http://www.cprogramming.com/boardfaq.html#random
Or search the board
B.t.w. It's almost impossible to make the numbers pure random.
If you are using linux: http://nodevice.com/sections/ManIndex/man1271.html
-
Here is the code:
Code:
int random(int low, int high)
{
srand(time(NULL));
return low + rand()%(high-low+1)
}
And with GetTickCount() it is almost the same, you just replace the
srand(time(NULL)) with srand(GetTickCount()).
B.T.W. What is the best solution?
-
You should only seed the random generator once per program:
Code:
int random(int Range)
{
return rand() % Range;
}
int main()
{
srand(time(NULL));
cout << random(10);
return 0;
}
-
Ok, I will test that as soon as I can. I'm in school right now so I will post if it worked when I come home.:)
-
It works better now. :D
Thanks Magos and Monster for your help. :)