b r a y d e n . o r g / Software

/ WebHome / LanguagePages / RubyLanguage / RubyProgrammingExamples / RandomNormalDistribution

This Web


WebHome  
Topic List  
Web Statistics 

All Webs


Books
Main
Random
Software
TWiki  

brayden.org


Home
Monthly Digest
Today's Links
Resumé
Reading List
Books RSS
Random RSS
Software RSS

Other


Dale's Blog

currently-reading
TextDrive

Normally-Distributed Random Numbers

... using the Box-Müller transform

Found at comp.lang.php on 2002/11/22 and implemented in php:

exercise: rewrite in RubyLanguage

$done = false;
while (!$done)
{
    $u1 = rand() / getrandmax();
    $u2 = rand() / getrandmax();

    // Set V1 = 2U1 - 1, V2 = 2U2 -1, S = V1^2 + V2^2

    $v1 = 2*$u1 -1;
    $v2 = 2*$u2 -1;
    $s = $v1 * $v1 + $v2 * $v2;

    // If S > 1, return to step 1
    if ($s <= 1)
        $done = true;
}

// Now return the independent unit normals
$x = sqrt(-2 * log($s) / $s) * $v1;
$y = sqrt(-2 * log($s) / $s) * $v2;

 
 
Current Rev: r1.3 - 25 Jun 2003 - 05:54 GMT - DaleBrayden, Revision History:Diffs | r1.3 | > | r1.2 | > | r1.1
© 2003-2011 by the contributing authors.