saving and random generator

Started by
3 comments, last by hello_there 22 years, 5 months ago
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
____________________________________________________________How could hell be worse?
Advertisement
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
For pausing, I believe there is a sleep() function somewhere... windows.h?
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
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 99int random = rand() % 100; 



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

This topic is closed to new replies.

Advertisement