Problems? Is your data what you think it is? PerlMonks

### Answer: How do I get random numbers that follow standard distribution?

 on Aug 16, 2000 at 13:22 UTC Need Help??

Q&A > math > How do I get random numbers that follow standard distribution? - Answer contributed by Anonymous Monk

```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 Answer: How do I get random numbers that follow standard distribution?
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.

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2020-10-27 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite web site is:

Results (258 votes). Check out past polls.

Notices?