in reply to Curved Random Distribution
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.sub almost_normal { my ($mean, $variance) = @_; return sqrt($variance) * atan2( rand(2) - 1, 1 ) + $mean; }
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):
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.#!/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;
blokhead
|
|---|