#### Archived

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

# saving and random generator

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

## 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 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 on other sites
For pausing, I believe there is a sleep() function somewhere... windows.h?

##### Share on other sites

So it''s not particularly accurate

##### 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 99int random = rand() % 100;

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

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
frob
13

• 9
• 15
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632602
• Total Posts
3007363

×