Pathologically Eclectic Rubbish Lister PerlMonks

### geometric random distributions

by kingkongrevenge (Scribe)
 on Jun 08, 2007 at 03:24 UTC Need Help??
kingkongrevenge has asked for the wisdom of the Perl Monks concerning the following question:

boost::random for C++ has a bunch of classes for making random numbers with non-linear distributions: docs. You can get random numbers biased on a normal, triangle, or geometric distribution, for examples.

I can only find linear and normal distribution type random distribution modules on CPAN. If the geometric distribution equivalent exists, I don't understand how I'm supposed to find it. Does it exist? If not I guess I might write my first CPAN module.

Replies are listed 'Best First'.
Re: geometric random distributions
by Zaxo (Archbishop) on Jun 08, 2007 at 03:35 UTC

Math::Random has a bunch of rng's with nonlinear distributions.

After Compline,
Zaxo

Re: geometric random distributions
by lin0 (Curate) on Jun 08, 2007 at 03:47 UTC

The book Mastering Algorithms with Perl by Jon Orwant, Jarkko Hietaniemi, and John Macdonald provides the code to compute several probability distributions (the geometric distribution included). The source code is available at http://examples.oreilly.com/maperl/ (once you uncompress the file, the code will be in the file "distributions" inside the ch14 folder).

I hope this helps.

Cheers,

lin0
Re: geometric random distributions
by Moron (Curate) on Jun 08, 2007 at 07:18 UTC
It's usually a trivial matter to create a geometric random number generator. For example, this expression will produce a random number in the range 0 to 1 with a (update co-)sinusoidal probability density function:
```    use Math::Trig; # to get PI
my \$sinerand = sin( rand() * PI );
(update: the probability density function of a random number generator is the derivative of the generating function, so in this case a cosine density shape is therefore being produced.)

A simple integration is also needed for the triangular distribution: Its probability density function has a smooth gradient so the generator satisfies the equation dy/dx = gradient. So integrating that straight line function, gives the generating function: 1 - (rand() ** (1+gradient))) would give a random number with a triangular probability density function coming out.

The reason the normal distribution is an exception deserving a special method is that its cumulative distribution function isn't analytic but can only be expressed as an integral equation. (Update: or as a Taylor Series.) You would need to achieve an impossible integration (in closed form) to compute the generating function that can produce a normal probability density function as its output. In practice some kind of approximation has to be used (Update: usually by taking the first few terms of the infinite Taylor series - although I have to say the module either doesn't or doesn't obviously do that!)

__________________________________________________________________________________

Re: geometric random distributions
by injunjoel (Priest) on Jun 08, 2007 at 16:41 UTC
You might want to look into PDL as well.
These are just a few of the modules available.

-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: geometric random distributions
by silverrock (Initiate) on Aug 22, 2011 at 03:37 UTC
int(log(1- rand)/log(1 - \$p)) will give you a random integer from a geometric distribution with probability \$p. \$p must, of course, be between 0 and 1.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://619944]
Approved by GrandFather
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-03-18 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (228 votes). Check out past polls.

Notices?