![]() |
| | #1 |
| Registered User Join Date: Nov 2002
Posts: 18
| 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. |
| Caze is offline | |
| | #2 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| Post the code!
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #3 |
| Me want cookie! Join Date: Dec 2001
Posts: 680
| 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 |
| Monster is offline | |
| | #4 |
| Registered User Join Date: Nov 2002
Posts: 18
| Here is the code: Code: int random(int low, int high)
{
srand(time(NULL));
return low + rand()%(high-low+1)
}
srand(time(NULL)) with srand(GetTickCount()). B.T.W. What is the best solution? |
| Caze is offline | |
| | #5 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| 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;
}
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #6 |
| Registered User Join Date: Nov 2002
Posts: 18
| 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. |
| Caze is offline | |
| | #7 |
| Registered User Join Date: Nov 2002
Posts: 18
| It works better now. ![]() Thanks Magos and Monster for your help. |
| Caze is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Issue w/ Guess My Number Program | mkylman | C++ Programming | 5 | 08-23-2007 01:31 AM |
| Good Random Number Generator | MethodMan | C Programming | 4 | 11-18-2004 06:38 AM |
| How do I restart a random number sequence. | jeffski | C Programming | 6 | 05-29-2003 02:40 PM |
| how to link random number generator with cpu? | chris285 | C++ Programming | 5 | 04-28-2003 05:26 AM |
| Seeding Random Number Generator | zdude | C++ Programming | 2 | 09-05-2002 03:10 PM |