C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2002, 04:03 AM   #1
Registered User
 
Caze's Avatar
 
Join Date: Nov 2002
Posts: 18
Angry 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.
Caze is offline   Reply With Quote
Old 12-03-2002, 04:09 AM   #2
Confused
 
Magos's Avatar
 
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   Reply With Quote
Old 12-03-2002, 04:16 AM   #3
Me want cookie!
 
Monster's Avatar
 
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   Reply With Quote
Old 12-03-2002, 05:17 AM   #4
Registered User
 
Caze's Avatar
 
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)
}
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?
Caze is offline   Reply With Quote
Old 12-03-2002, 05:20 AM   #5
Confused
 
Magos's Avatar
 
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   Reply With Quote
Old 12-03-2002, 05:27 AM   #6
Registered User
 
Caze's Avatar
 
Join Date: Nov 2002
Posts: 18
Smile

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   Reply With Quote
Old 12-03-2002, 08:10 AM   #7
Registered User
 
Caze's Avatar
 
Join Date: Nov 2002
Posts: 18
It works better now.
Thanks Magos and Monster for your help.
Caze is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:41 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22