Archived

This topic is now archived and is closed to further replies.

saving and random generator

This topic is 5897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a problem with the random generator in c++. for starters how do you use it. It never seems to work. can someone please help me. and also is there a function in c++ to make the computer wait/pause for a few secs. please help me!!! hmmm interesting

Share this post


Link to post
Share on other sites
hmm
rand(0) returns a random integer.
if you want a random number say between 100 and 150 then do like this:
randomInteger = 100 + ( rand(0) % 50 );

or if you want a random decimal value beteween -1.00 and 1.00 then:

randomDecimal = -1.00f + ((float)(rand(0) % 100) / 50.0f)

(I assume u r familiar with the '%' operator, if not: ask)

To make the puter pause..
you can:

int startTime = time(0);
while((time(0) - startTime) < 1) // pauses about one sec
{
// wait
}

but there are probably several ways to do this, maybe someone else has a smarter algorithm for you.

Edited by - Jesper T on October 22, 2001 2:17:20 AM

Share this post


Link to post
Share on other sites
sleep() isn''t particularly good. It essentially tells windows to jump to another thread and come back to this one when your ready AFTER this amount of time.

So it''s not particularly accurate

Share this post


Link to post
Share on other sites
Use timeGetTime() for pausing - wait until the value it returns is greater than or equal to your desired sleep time.

rand() also doesn''t take any parameters; you need to seed the random number generator using srand(). rand() is guaranteed to generate identical sequences of pseudorandom numbers for the same seed, so it''s often a good idea to save your seed. Also, the more random the seed, the more "random" the sequence of numbers. A good way to get a seed is to divide the instantaneous system tick count by 120 or so (remember to save this seed!)

unsigned int seed = timeGetTime()/120;
srand(seed);
// get a "random" number between 0 and 99
int random = rand() % 100;




To you it''s a Bently, to me it''s a blue car...
"Diddy"
P.Diddy

Share this post


Link to post
Share on other sites