If all you care about is that your probability distribution qualitatively looks like the classic bell-curve, you can use this quick-and-dirty approximation:
sub almost_normal { my ($mean, $variance) = @_; return sqrt($variance) * atan2( rand(2) - 1, 1 ) + $mean; }
The reason it works is because the normal distribution is shaped like exp(-x2). A reasonable first-order approximation of exp(x) is (1+x), so we can try using 1/(1+x2) to approximate the normal curve. This function's integral (accumulating probability distribution) is none other than arctan.

This gives you a bell-shaped curve in the shape of 1/(1+x2), which is qualitatively "bell-shaped", but has a lot more probability in its "tails" than the normal distribution.

Update: Augh, this is completely backwards, please ignore. In any case, it should be taking tangents of a random angle (or something?), but I can't get the scaling factors to work out right now. So much for a quick and easy alternative. ;)

Can someone who actually knows this math say (correctly) what I was trying to say? I want the accumulating probability distribution to be shaped like arctan.

Update 2: (trying to salvage this node) After thinking about it for a while, this seems to work (stealing code from BrowserUk's reply):

#!/usr/bin/perl -slw my $PI = 4*atan2(1,1); sub almost_normal { my ($mean, $variance) = @_; my $x = rand(2*$PI); return sqrt($variance) * sin($x)/cos($x) + $mean; } our $PRECISION ||= 1; our $ITERS = 1000 * $PRECISION; my %plot; $plot{ int( $PRECISION * almost_normal( 100, 5 ) ) / $PRECISION }++ for 1 .. $ITERS; print "Rand 100 +-5"; printf "%7.2f : %-3d : %s\n", $_, $plot{ $_ }, '#' x ( $plot{ $_ } / $ITERS * 100 * $PRECISION ) for sort{ $a <=> $b } keys %plot;
The variance is pretty wide, though ... you might be well-served to scale it down a bit. Although it's nice that the tails do extend really far.

blokhead


In reply to Re: Curved Random Distribution by blokhead
in thread Curved Random Distribution by Adam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.