in reply to How do I get random numbers that follow standard distribution?

sub norm{ my $v1,$v2,$r; if( defined $v2 ){ $v1 = $v2; undef $v2; }else{ do{ $v1=rand(2)-1; $v2=rand(2)-1; $r = $v1*$v1+$v2*$v2; }while( $r >= 1 || $r == 0 ); $r = sqrt(-2*(log $r)/$r); $v1 *= $r; $v2 *= $r; } return $v1; }
  • Comment on Re: How do I get random numbers that follow standard distribution?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How do I get random numbers that follow standard distribution?
by jimbojones (Friar) on Feb 16, 2005 at 23:01 UTC
    Just to let others know: this method appears to be from "Numerical Recipes in C", which is now available, full-form, online. See c7-2.pdf.

    this also features recipes for other distributions, plus lots of other good stuff ....

    - j

Re: Answer: How do I get random numbers that follow standard distribution?
by Roy Johnson (Monsignor) on Feb 17, 2005 at 15:31 UTC
    This is a buggy translation from Knuth. $v2 will never be defined; it should have been declared outside the sub.

    Caution: Contents may have been coded under pressure.