]> gitweb.factorcode.org Git - factor.git/commitdiff
random: Add poisson-random-float.
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 7 Dec 2012 17:29:48 +0000 (09:29 -0800)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 31 Dec 2012 19:02:56 +0000 (11:02 -0800)
basis/random/random.factor

index 5c3992dd5be7a15c77043d1447d97f14931663e5..5571fccd3d791184d6b50430c572619a0a967588 100644 (file)
@@ -287,6 +287,12 @@ M: float random [ f ] [ 0.0 swap uniform-random-float ] if-zero ;
 : power-random-float ( alpha -- n )
     [ random-unit log e^ 1 swap - ] dip recip ^ ;
 
+! Box-Muller
+: poisson-random-float ( mean -- n )
+    [ -1 0 ] dip
+    [ 2dup < ]
+    [ [ 1 + ] 2dip [ random-unit log neg + ] dip ] while 2drop ;
+
 {
     { [ os windows? ] [ "random.windows" require ] }
     { [ os unix? ] [ "random.unix" require ] }