1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data arrays assocs
4 byte-arrays byte-vectors combinators combinators.short-circuit
5 fry io.backend io.binary kernel locals math math.bitwise
6 math.constants math.functions math.order math.ranges namespaces
7 sequences sequences.private sets summary system vocabs hints
11 SYMBOL: system-random-generator
12 SYMBOL: secure-random-generator
13 SYMBOL: random-generator
15 GENERIC# seed-random 1 ( tuple seed -- tuple' )
16 GENERIC: random-32* ( tuple -- r )
17 GENERIC: random-bytes* ( n tuple -- byte-array )
19 M: object random-bytes* ( n tuple -- byte-array )
20 [ [ <byte-vector> ] keep 4 /mod ] dip
21 [ pick '[ _ random-32* int <ref> _ push-all ] times ]
24 [ 2drop ] [ random-32* int <ref> swap head append! ] if
25 ] bi-curry bi* B{ } like ;
27 HINTS: M\ object random-bytes* { fixnum object } ;
29 M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ;
31 ERROR: no-random-number-generator ;
33 M: no-random-number-generator summary
34 drop "Random number generator is not defined." ;
36 M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
38 M: f random-32* ( obj -- * ) no-random-number-generator ;
40 : random-32 ( -- n ) random-generator get random-32* ;
42 TYPED: random-bytes ( n: fixnum -- byte-array: byte-array )
43 random-generator get random-bytes* ; inline
47 :: ((random-integer)) ( bits obj -- n required-bits )
48 obj random-32* 32 bits 32 - [ dup 0 > ] [
49 [ 32 shift obj random-32* + ] [ 32 + ] [ 32 - ] tri*
52 : (random-integer) ( n obj -- n' )
53 [ dup next-power-of-2 log2 ] dip ((random-integer))
56 : random-integer ( n -- n' )
57 random-generator get (random-integer) ;
61 : random-bits ( numbits -- r ) 2^ random-integer ;
63 : random-bits* ( numbits -- n )
64 1 - [ random-bits ] keep set-bit ;
66 GENERIC: random ( obj -- elt )
68 M: integer random [ f ] [ random-integer ] if-zero ;
72 [ length random-integer ] keep nth
75 : randomize-n-last ( seq n -- seq )
76 [ dup length dup ] dip - 1 max '[ dup _ > ]
77 [ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
80 : randomize ( seq -- randomized )
81 dup length randomize-n-last ;
83 ERROR: too-many-samples seq n ;
85 : sample ( seq n -- seq' )
86 2dup [ length ] dip < [ too-many-samples ] when
87 [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
90 : delete-random ( seq -- elt )
91 [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
93 : with-random ( tuple quot -- )
94 random-generator swap with-variable ; inline
96 : with-system-random ( quot -- )
97 system-random-generator get swap with-random ; inline
99 : with-secure-random ( quot -- )
100 secure-random-generator get swap with-random ; inline
104 : (uniform-random-float) ( min max obj -- n )
105 [ 4 4 ] dip [ random-bytes* uint deref >float ] curry bi@
107 [ over - 2.0 -64 ^ * ] dip
112 : uniform-random-float ( min max -- n )
113 random-generator get (uniform-random-float) ; inline
115 : random-unit ( -- n )
116 0.0 1.0 uniform-random-float ; inline
118 : random-units ( length -- sequence )
119 random-generator get '[ 0.0 1.0 _ (uniform-random-float) ] replicate ;
121 : random-integers ( length n -- sequence )
122 random-generator get '[ _ _ (random-integer) ] replicate ;
124 : (cos-random-float) ( -- n )
125 0. 2pi uniform-random-float cos ;
127 : (log-sqrt-random-float) ( -- n )
128 random-unit log -2. * sqrt ;
130 : normal-random-float ( mean sigma -- n )
131 (cos-random-float) (log-sqrt-random-float) * * + ;
133 : lognormal-random-float ( mean sigma -- n )
134 normal-random-float e^ ;
136 : exponential-random-float ( lambda -- n )
137 random-unit log neg swap / ;
139 : weibull-random-float ( alpha beta -- n )
141 [ random-unit log neg ] dip
145 : pareto-random-float ( k alpha -- n )
146 [ random-unit ] dip recip ^ /f ;
148 :: (gamma-random-float>1) ( alpha beta -- n )
149 ! Uses R.C.H. Cheng, "The generation of Gamma
150 ! variables with non-integral shape parameters",
151 ! Applied Statistics, (1977), 26, No. 1, p71-74
152 2. alpha * 1 - sqrt :> ainv
153 alpha 4. log - :> bbb
156 0 :> r! 0 :> z! 0 :> result! ! initialize locals
159 [ 1. 4.5 log + + z 4.5 * - 0 >= ]
166 u1 1. u1 - / log ainv / :> v
174 : (gamma-random-float=1) ( alpha beta -- n )
175 nip random-unit log neg * ;
177 :: (gamma-random-float<1) ( alpha beta -- n )
178 ! Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
181 0 :> x! 0 :> p! ! initialize locals
184 random-unit x alpha 1 - ^ >
186 random-unit x neg e^ >
193 b p - alpha / log neg
195 ] do while x beta * ;
197 : gamma-random-float ( alpha beta -- n )
199 { [ over 1 > ] [ (gamma-random-float>1) ] }
200 { [ over 1 = ] [ (gamma-random-float=1) ] }
201 [ (gamma-random-float<1) ]
204 : beta-random-float ( alpha beta -- n )
205 [ 1. gamma-random-float ] dip over zero?
206 [ 2drop 0 ] [ 1. gamma-random-float dupd + / ] if ;
208 :: von-mises-random-float ( mu kappa -- n )
209 ! Based upon an algorithm published in: Fisher, N.I.,
210 ! "Statistical Analysis of Circular Data", Cambridge
211 ! University Press, 1993.
215 4. kappa sq * 1. + sqrt 1. + :> a
216 a 2. a * sqrt - 2. kappa * / :> b
217 b sq 1. + 2. b * / :> r
219 0 :> c! 0 :> _f! ! initialize locals
222 [ 2. c - c * < ] [ 1. c - e^ c * <= ]
225 random-unit pi * cos :> z
226 r z * 1. + r z + / _f!
230 mu 2pi mod _f cos random-unit 0.5 > [ + ] [ - ] if
233 :: (triangular-random-float) ( low high mode -- n )
234 mode low - high low - / :> c!
237 u c > [ 1. u - u! 1. c - c! swap ] when
238 [ - u c * sqrt * ] keep + ;
240 : triangular-random-float ( low high -- n )
241 2dup + 2 /f (triangular-random-float) ;
243 : laplace-random-float ( mean scale -- n )
244 random-unit dup 0.5 <
245 [ 2 * log ] [ 1 swap - 2 * log neg ] if * + ;
247 : cauchy-random-float ( median scale -- n )
248 random-unit 0.5 - pi * tan * + ;
250 : chi-square-random-float ( dof -- n )
251 [ 0.5 ] dip 2 * gamma-random-float ;
253 : student-t-random-float ( dof -- n )
254 [ 0 1 normal-random-float ] dip
255 [ chi-square-random-float ] [ / ] bi sqrt / ;
257 : inv-gamma-random-float ( shape scale -- n )
258 recip gamma-random-float recip ;
260 : rayleigh-random-float ( mode -- n )
261 random-unit log -2 * sqrt * ;
263 : gumbel-random-float ( loc scale -- n )
264 random-unit log neg log * - ;
266 : logistic-random-float ( loc scale -- n )
267 random-unit dup 1 swap - / log * + ;
269 : power-random-float ( alpha -- n )
270 [ random-unit log e^ 1 swap - ] dip recip ^ ;
273 { [ os windows? ] [ "random.windows" require ] }
274 { [ os unix? ] [ "random.unix" require ] }
277 "random.mersenne-twister" require