Advertisement

Randomize

Started by May 05, 2002 07:53 PM
13 comments, last by Ragadast 22 years, 3 months ago
quote: Original post by Kern
Carrot : if RAND_MAX == 10 and you call rand() % 3 the probability would be : 0=4 1=3 2=3. In the long run this makes for a big mistake and since that mistake is alway in the low number it imbalance things.


Good point,thanks.
<a href="http://www.purplenose.com>purplenose.com
There honestly should be a FAQ for this random stuff. It's asked every month here and every month there are ppl saying "use rand()%x" and other ppl correcting them.
* S I G H *

Although in a game, I don't think a slightly biased distribution is noticeable.

[edited by - civguy on July 22, 2002 10:25:08 AM]
Advertisement
Before you can use rand() you need to seed the random number generator with srand(TIME(NULL)) or similar.
32767?!?!?

What do you mean windows is limited? hehehehe
in my text book, this is how they say you get a random int:
// place this at top of int_main
void rand_seed()
{ int seed = static_cast<int>(time(0));
srand(seed);
}

// gives you random int in the range you specify
int rand_int(int bottom_range, int top_range)
{ return bottom_range + rand() % (top_range - bottom_range + 1);
}

This topic is closed to new replies.

Advertisement