# Random number generator

• 12-03-2002
Caze
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.
• 12-03-2002
Magos
Post the code!
• 12-03-2002
Monster
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
• 12-03-2002
Caze
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?
• 12-03-2002
Magos
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; }```
• 12-03-2002
Caze
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.:)
• 12-03-2002
Caze
It works better now. :D
Thanks Magos and Monster for your help. :)