rand() question

Started by
13 comments, last by Xai 19 years, 5 months ago
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)
Advertisement
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."
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
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.
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)
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)
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
using namespace std;

int main()
{
int nRandom;
srand((unsigned int)time(0)); // this command is only used once
nRandom = rand();// used everytime a rand number is needed
nRandom=nRandom%100;
return 0;
}

this gives a random number between 1-100 that is a bit more random.
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.
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!

This topic is closed to new replies.

Advertisement