## Probability distributions

-ryan

ryanmsmith Wrote:I'm writing an app with some aspects of simulation (queueing theory). I'd really like to generate random numbers according to the Poisson distribution and was wondering if there is a straight-forward library out there to use for that. I've searched and found hints of solutions. Uniform and Normal distributions are a piece of cake, but do you do for others?

-ryan

I thought up this trick a few years back, though I'm sure it's a widely known method. It will work to generate random numbers following whatever distribution you'd like. I can send you my code for this if you want.

First generate a random floating point number between 0 and 1 using your uniform distribution. Let's call this number x.

Now, to get a random number that follows the desired distribution, let's call it y, find y such that the integral of your distribution function from minus infinity to y is equal to x.

This method can be extremely difficult to compute exactly, especially if your distribution function is complicated. The reason is that in order to find y you need to integrate your PDF from minus infinity until the integral meets or exceeds the value x. In most cases you will have to use numerical methods in order to approximate the integral, because you won't be able to solve the equation analytically. This process can be sped up if you keep a table of integrated values that you can re-use each time you want a random number.

backslash Wrote:If speed is more of a concern than accuracy you could use a the output of rand() (or your conventional uniform random number generator of choice) to index a look up table. Perhaps a little linear interpolation between indices would expand the range of possible results without eating too much RAM, and shouldn't colour the results too much).

That's the exact optimization I would recommend.

Use the formula I gave to generate the lookup table.