Page 1 of 1

Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 11:14 am
by MythX
I think that's what's it's called. But what I'm finding on it isn't helping me out. Here's the end result of what I'm looking for.

I'd like to generate random numbers with a distribution that would fit within a bell curve. Say we generate numbers from 0 to 99. I'd like most of them to be 50, tapering in either direction such that a 0 or 99 would be extremely rare.

I've thought about calculating the area beneath the curve of a hyperbolic function, assigning the value of the x coordinate to the function, but don't think it would perform well.

I've found a lot of documents and examples of the Gaussian distribution, but nothing is helping me get the distribution I need.

Has anyone done this before?

Re: Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 12:30 pm
by Christopher
You probably want to generate random numbers in the range 0..99 and then in the rage 1..98 and then 2..97 to create your entire set of random values. If you just step 0..49 you will get a pyramid -- not a bell, so you need a parametric equation so your steps give the distribution you want.

Re: Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 12:33 pm
by AbraCadaver

Re: Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 12:41 pm
by MythX
Christopher wrote:You probably want to generate random numbers in the range 0..99 and then in the rage 1..98 and then 2..97 to create your entire set of random values. If you just step 0..49 you will get a pyramid -- not a bell, so you need a parametric equation so your steps give the distribution you want.
I like this idea. It's not exactly the parametric equation driven function I was imagining, but it may very well serve the purpose. I'll toy with it, and post it if I come up with something functional.

Thanks

Re: Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 12:42 pm
by MythX
AbraCadaver wrote:Something like this may help: http://stackoverflow.com/questions/5188 ... m-with-php
I actually already found this page. Unfortunately, I was unable to get what I was looking for out of it.

Thanks for the reply though.

Jason

Re: Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 3:53 pm
by Eric!
I think the distribution you are looking for is Gaussian but in a discrete form. Christopher's idea won't give you a smooth curve but it will have a bell-shaped tendency.

If you have 100 numbers all the probabilities need to equal 1 when summed together. So a flat curve would be 1/100 for every value and P(X)=1/100. This is a typical random event like rolling a 100 sided die.

If you want to simulate an event with a Gaussian shape use this formula:
f(x) = a*e^-[(x-b)^2 / (2*c^2)]

a,b,c>0 and b is the position of the center of your peak and e is the natural logarithm. c is related to the full width at 1/2 maximum of the peak (in engineering terms the bandwidth) and you can just set a=1. Put your x values in and quantize it for discrete numbers. An example would be a=1, b=50, c=20.

You will then have an array of probabilities P for each X. You can then build a data set with 10x more numbers (say 1000) using this distribution. So if P(0)=0.002 take 0.002*1000 elements and make an array with rand[0]=0 and rand[1]=0 (two times). Then continue this for all values of X, for example P(50)=.08 .... take .08*1000 and add 80 elements of the number 50 to your array. When you are done you should have 1000 elements in your new dataset. So now when you pick an element from your data at random you should slowly see the bell-shape form if you record the results.

At least I think this is what you're trying to do...!

Good luck!

Re: Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 4:07 pm
by Eric!
Oh, I found a discrete version of the gausian function here
http://en.wikipedia.org/wiki/Discrete_G ... ian_kernel

Re: Probabilistic Distribution Random Numbers

Posted: Sat Sep 17, 2011 5:42 pm
by Eric!
Sorry, couldn't stop myself for some reason. I made the echo statements expand as the array of data is calculated so you can see the discrete bell shape form visually. Due to rounding you don't end up with 1000 numbers in the data set. You can set this to 10000 for higher resolution and you can play with $c making it smaller to make the peak tighter.

To use the data set just do this: $Probabilistic_Dist_Random_Number=$data[$rand]; where $rand is randomly selected from a range of 0 to your count($data)-1.

Code: Select all

$max_numbers = 100; // go 0...99
$max_data = 1000; // sample data to generate

$a = 1 / $max_numbers; // scale factor = value at peak of the curve
$b = $max_numbers / 2;  // center curve in middle of data range
$c = 30.0;  // width of the half-height at the center

for ($x = 0; $x < $max_numbers; $x++) {
    // build curve
    $P[$x] = $a * exp(-1 * ((pow(($x - $b), 2) / (2 * pow($c, 2)))));  //f(x) = a*e^-[(x-b)^2 / (2*c^2)]
}
// apply numbers to curve to extract data
for ($x = 0; $x < $max_numbers; $x++) {
    $add = round($P[$x] * $max_data, 0); // make whole number
    echo "<br>P[$x]=" . number_format($P[$x],8) . " add=" . $add . " elements: ";
    if ($add > 0) { // ignore data that has 0 chance of occuring in data set
        for ($i = 0; $i < $add; $i++) {
            $data[] = $x;
            echo ($i + 1) . " ";
        }
    }
}