From 9e9d830ec0acd5d8646f7d15db4ac9a24019d8db Mon Sep 17 00:00:00 2001
From: John Benediktsson <mrjbq7@gmail.com>
Date: Wed, 18 Apr 2012 14:37:27 -0700
Subject: [PATCH] random: adding rayleigh, gumbel, logistic, and power random
 floats.

---
 basis/random/random-tests.factor | 17 +++++++++++++++--
 basis/random/random.factor       | 12 ++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/basis/random/random-tests.factor b/basis/random/random-tests.factor
index f22b02a147..3bbcd943be 100644
--- a/basis/random/random-tests.factor
+++ b/basis/random/random-tests.factor
@@ -1,5 +1,5 @@
-USING: random sequences tools.test kernel math math.functions
-sets grouping random.private math.statistics ;
+USING: random sequences tools.test kernel math math.constants
+math.functions sets grouping random.private math.statistics ;
 IN: random.tests
 
 [ 4 ] [ 4 random-bytes length ] unit-test
@@ -76,3 +76,16 @@ IN: random.tests
     50000 [ 2 3 laplace-random-float ] replicate
     [ mean 2 .2 ~ ] [ std 2 sqrt 3 * .2 ~ ] bi
 ] unit-test
+
+{ t t }
+[
+    50000 [ 12 rayleigh-random-float ] replicate
+    [ mean pi 2 / sqrt 12 * .2 ~ ]
+    [ std 2 pi 2 / - sqrt 12 * .2 ~ ] bi
+] unit-test
+
+{ t t }
+[
+    50000 [ 3 4 logistic-random-float ] replicate
+    [ mean 3 .2 ~ ] [ std pi 4 * 3 sqrt / .2 ~ ] bi
+] unit-test
diff --git a/basis/random/random.factor b/basis/random/random.factor
index cdce8e4afa..a13f4065d7 100644
--- a/basis/random/random.factor
+++ b/basis/random/random.factor
@@ -242,6 +242,18 @@ ERROR: too-many-samples seq n ;
 : inv-gamma-random-float ( shape scale -- n )
     recip gamma-random-float recip ;
 
+: rayleigh-random-float ( mode -- n )
+    random-unit log -2 * sqrt * ;
+
+: gumbel-random-float ( loc scale -- n )
+    random-unit log neg log * - ;
+
+: logistic-random-float ( loc scale -- n )
+    random-unit dup 1 swap - / log * + ;
+
+: power-random-float ( alpha -- n )
+    [ random-unit log exp 1 swap - ] dip recip ^ ;
+
 {
     { [ os windows? ] [ "random.windows" require ] }
     { [ os unix? ] [ "random.unix" require ] }