This weekend Code Project posted an updated version of my article Simple Random Number Generation. The article comes with C# source code for generating random samples from the following distributions.
- Cauchy
- Chi square
- Exponential
- Inverse gamma
- Laplace (double exponential)
- Normal
- Student t
- Uniform
- Weibull
After I submitted the revised article I realized I could have easily included a beta distribution generator. To generate a sample from a beta(a, b) distribution, generate a sample u from gamma(a, 1) and a sample v from gamma(b, 1) and return u/(u+v). (See why this works here.)
This isn’t the most efficient beta generator possible, especially for some parameters. But it’s not grossly inefficient either. Also, it’s very simple, and the code in that article emphasizes simplicity over efficiency.
The code doesn’t use advanced C# features; it could easily be translated to other languages.
Related links:
Wanted to drop a quick note that I ported this to Ruby. My tests are less sophisticated, it’s on my TODO to do it up right, but the random number generation appears to be working correctly. And I added the beta distribution per your comment above.
http://github.com/ealdent/simple-random
or view the gem at:
http://rubygems.org/gems/simple-random
I just wanted to ask a question regarding the notes by Marsaglia, to which you have linked in your post on Code Project. In his notes he introduces 8 different PRNGs with some simple properties, however many on these generators appear to have periods in the hundreds to thousands range. For a 32-bit integer generator these numbers are pretty low. Is this due to a typo or have I misunderstood something?