Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

geometric random distributions

by kingkongrevenge (Scribe)
on Jun 08, 2007 at 03:24 UTC ( #619944=perlquestion: print w/replies, xml ) 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,

Re: geometric random distributions
by lin0 (Curate) on Jun 08, 2007 at 03:47 UTC
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!)


    ^M Free your mind!

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.

    "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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://619944]
Approved by GrandFather
[tye]: I'm not using SELinux and it certainly appears to disagree with you. shrug
[tye]: Since you brought up /proc, oiskuu, I didn't see you respond to my suggestion of 'loginuid'. Does your /proc not have such?
[oiskuu]: Hm. that depends on kernel compiled with CONFIG_AUDITSYSCAL L

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2017-06-23 19:52 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.