Skip to main content
gamedev.net
🔒 Locked

rand() question

Started by amish1234 13 replies 1.4k views
Original Post
amish1234
amish1234
I am working on a function where X percent of the time A happens and Y percent of the time B happens. However, I can't just do something like

if( rand() % 100 < X)
because rand() gives a gaussian distibution. (I am under the impression that this means rand()%100 will give 50 most commonly and 1 and 99 least commonly). So is their either
  1. A random function with a flat distribution curve OR
  2. A formula that could give me W and Z such that W < rand()%100 < Z is true X percent of the time rand()%100 is performed.
Thanks a lot.
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
DarkHamster
DarkHamster
rand() does not give a gaussian distribution, it has a uniform distribution.


"There is no dark side of the moon really,
As a matter of fact, its all dark."
Fruny
Fruny
Hrm... rand() does give a uniform distribution.

If yours is broken, use boost::random
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Woodsman
Woodsman
This seems to indicate that it is a uniform distribution.

It would seem silly to have it be a gaussian distribution... (And I've never noticed that behavior...)
If a plant cannot live according to its nature, it dies; so a man.
amish1234
amish1234
Okay, well I'm going to kill the guy who told me it gives a gaussian distribution.
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
caesar4
caesar4
if u sum randoms, then u get a normal distribution
example
rand()+rand()+rand()+ ... = normally distributed rands
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
GameDev135
GameDev135
actually, i believe if you sum ANY random variables--or enough of them anyway, it will be a gaussian (normal) distribution. I think this is the Weak Law of Large Numbers. Like if you flip a coin 100,000 times, the number of heads will follow a normal distribution (actually you don't need to flip it nearly that many times for it to be a normal distribution)

edit: note that all the random variables have to be of an identical distribution for this to be true (assuming it is in the first place)--that is, you can't sum gaussian random variables with uniform and expect it to be normal (makes sense, but worth explicetly saying)
Feel free to email me at NYYanks432@hotmail.com if you have any questions
_DarkWIng_
_DarkWIng_
Using rand() will give you uniform distribution, but rand()%100 will not. But we don't want to get in this discussion again, right?
You should never let your fears become the boundaries of your dreams.
grekster
grekster
Quote:
Original post by _DarkWIng_
Using rand() will give you uniform distribution, but rand()%100 will not. But we don't want to get in this discussion again, right?


if rand() gives you a uniform distribution, why doesnt rand()%100?
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
_DarkWIng_
_DarkWIng_
Quote:
Original post by grekster
if rand() gives you a uniform distribution, why doesnt rand()%100?


Imagine rand() would return value in range 0 to 127. So if you did rand()%100 you get 2x higher probability of getting a number in 0 to 27 range than in 28 to 99 range. Now, rand() has a range bigger then 127 but I hope you get the idea.
You should never let your fears become the boundaries of your dreams.
fiskbil
fiskbil
Certain implementations of rand() also have less randomness in the least significant bits and thus a modulo is not a good way to use it. Instead use

range*rand()/(float)INT_MAX

or

range*rand()/static_cast<float>(std::numeric_limits<int>::max())
Enigma
Enigma
Actually, it would be much better to use RAND_MAX than the maximum integer value, since the two are usually different.

Enigma
fiskbil
fiskbil
Quote:
Original post by Enigma
Actually, it would be much better to use RAND_MAX than the maximum integer value, since the two are usually different.

Enigma


Indeed. Sorry about that.
Xai
Xai
on visual c++ 7.1 i believe RAND_MAX is the same as the highest 16 bit signed number (even though rand() returns a 32 bit number on this platform) ... so it is always good to check what RAND_MAX is on your platform before making too many assumptions ...

as for the distrobution of rand()%100, it should be as close to normal as rand() is, simply because the range is 320 something times greater than 100, so the leftover part will affect your distrobution by a factor of less than 0.3%, which is as close as you are going to get to normal in a pseudo random generator anyway ...

If you tried to generate a random number between 0-25,0000 then things would be different, because, as mentioned the leftovers would cause the low numbers to be twice as likely as the high numbers ...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.