]> gitweb.factorcode.org Git - factor.git/commitdiff
uniform and normal distributed random floats. uniform is done the lame way for now
authorJoe Groff <arcata@gmail.com>
Wed, 6 May 2009 18:22:53 +0000 (13:22 -0500)
committerJoe Groff <arcata@gmail.com>
Wed, 6 May 2009 18:22:53 +0000 (13:22 -0500)
basis/random/random.factor

index d972e1e7ac6e454ef689721b793a3af268ed549a..e3f1ecccb90f91af8d8631fcc14c2bb9cbacc176 100755 (executable)
@@ -3,7 +3,7 @@
 USING: alien.c-types kernel math namespaces sequences
 io.backend io.binary combinators system vocabs.loader
 summary math.bitwise byte-vectors fry byte-arrays
-math.ranges ;
+math.ranges math.constants math.functions ;
 IN: random
 
 SYMBOL: system-random-generator
@@ -69,6 +69,17 @@ PRIVATE>
 : with-secure-random ( quot -- )
     secure-random-generator get swap with-random ; inline
 
+: uniform-random-float ( min max -- n )
+    64 random-bits >float [ over - 2.0 -64 ^ * ] dip
+    * + ;
+
+: normal-random-float ( mean sigma -- n )
+    0.0 1.0 uniform-random-float
+    0.0 1.0 uniform-random-float
+    [ 2 pi * * cos ]
+    [ 1.0 swap - log -2.0 * sqrt ]
+    bi* * * + ;
+
 USE: vocabs.loader
 
 {