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 -- n required-bits )
48 [ random-32 32 ] dip 32 - [ dup 0 > ] [
49 [ 32 shift random-32 + ] [ 32 + ] [ 32 - ] tri*
52 : random-integer ( n -- n' )
53 dup next-power-of-2 log2 (random-integer)
58 : random-bits ( numbits -- r ) 2^ random-integer ;
60 : random-bits* ( numbits -- n )
61 1 - [ random-bits ] keep set-bit ;
63 GENERIC: random ( obj -- elt )
65 M: integer random [ f ] [ random-integer ] if-zero ;
69 [ length random-integer ] keep nth
72 : randomize-n-last ( seq n -- seq )
73 [ dup length dup ] dip - 1 max '[ dup _ > ]
74 [ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
77 : randomize ( seq -- randomized )
78 dup length randomize-n-last ;
80 ERROR: too-many-samples seq n ;
82 : sample ( seq n -- seq' )
83 2dup [ length ] dip < [ too-many-samples ] when
84 [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
87 : delete-random ( seq -- elt )
88 [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
90 : with-random ( tuple quot -- )
91 random-generator swap with-variable ; inline
93 : with-system-random ( quot -- )
94 system-random-generator get swap with-random ; inline
96 : with-secure-random ( quot -- )
97 secure-random-generator get swap with-random ; inline
99 : uniform-random-float ( min max -- n )
100 4 random-bytes uint deref >float
101 4 random-bytes uint deref >float
103 [ over - 2.0 -64 ^ * ] dip
106 : random-unit ( -- n )
107 0.0 1.0 uniform-random-float ; inline
109 : random-units ( length -- sequence )
110 [ random-unit ] replicate ;
112 : random-integers ( length n -- sequence )
113 '[ _ random ] replicate ;
115 : (cos-random-float) ( -- n )
116 0. 2pi uniform-random-float cos ;
118 : (log-sqrt-random-float) ( -- n )
119 random-unit log -2. * sqrt ;
121 : normal-random-float ( mean sigma -- n )
122 (cos-random-float) (log-sqrt-random-float) * * + ;
124 : lognormal-random-float ( mean sigma -- n )
125 normal-random-float e^ ;
127 : exponential-random-float ( lambda -- n )
128 random-unit log neg swap / ;
130 : weibull-random-float ( alpha beta -- n )
132 [ random-unit log neg ] dip
136 : pareto-random-float ( k alpha -- n )
137 [ random-unit ] dip recip ^ /f ;
139 :: (gamma-random-float>1) ( alpha beta -- n )
140 ! Uses R.C.H. Cheng, "The generation of Gamma
141 ! variables with non-integral shape parameters",
142 ! Applied Statistics, (1977), 26, No. 1, p71-74
143 2. alpha * 1 - sqrt :> ainv
144 alpha 4. log - :> bbb
147 0 :> r! 0 :> z! 0 :> result! ! initialize locals
150 [ 1. 4.5 log + + z 4.5 * - 0 >= ]
157 u1 1. u1 - / log ainv / :> v
165 : (gamma-random-float=1) ( alpha beta -- n )
166 nip random-unit log neg * ;
168 :: (gamma-random-float<1) ( alpha beta -- n )
169 ! Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
172 0 :> x! 0 :> p! ! initialize locals
175 random-unit x alpha 1 - ^ >
177 random-unit x neg e^ >
184 b p - alpha / log neg
186 ] do while x beta * ;
188 : gamma-random-float ( alpha beta -- n )
190 { [ over 1 > ] [ (gamma-random-float>1) ] }
191 { [ over 1 = ] [ (gamma-random-float=1) ] }
192 [ (gamma-random-float<1) ]
195 : beta-random-float ( alpha beta -- n )
196 [ 1. gamma-random-float ] dip over zero?
197 [ 2drop 0 ] [ 1. gamma-random-float dupd + / ] if ;
199 :: von-mises-random-float ( mu kappa -- n )
200 ! Based upon an algorithm published in: Fisher, N.I.,
201 ! "Statistical Analysis of Circular Data", Cambridge
202 ! University Press, 1993.
206 4. kappa sq * 1. + sqrt 1. + :> a
207 a 2. a * sqrt - 2. kappa * / :> b
208 b sq 1. + 2. b * / :> r
210 0 :> c! 0 :> _f! ! initialize locals
213 [ 2. c - c * < ] [ 1. c - e^ c * <= ]
216 random-unit pi * cos :> z
217 r z * 1. + r z + / _f!
221 mu 2pi mod _f cos random-unit 0.5 > [ + ] [ - ] if
224 :: (triangular-random-float) ( low high mode -- n )
225 mode low - high low - / :> c!
228 u c > [ 1. u - u! 1. c - c! swap ] when
229 [ - u c * sqrt * ] keep + ;
231 : triangular-random-float ( low high -- n )
232 2dup + 2 /f (triangular-random-float) ;
234 : laplace-random-float ( mean scale -- n )
235 random-unit dup 0.5 <
236 [ 2 * log ] [ 1 swap - 2 * log neg ] if * + ;
238 : cauchy-random-float ( median scale -- n )
239 random-unit 0.5 - pi * tan * + ;
241 : chi-square-random-float ( dof -- n )
242 [ 0.5 ] dip 2 * gamma-random-float ;
244 : student-t-random-float ( dof -- n )
245 [ 0 1 normal-random-float ] dip
246 [ chi-square-random-float ] [ / ] bi sqrt / ;
248 : inv-gamma-random-float ( shape scale -- n )
249 recip gamma-random-float recip ;
251 : rayleigh-random-float ( mode -- n )
252 random-unit log -2 * sqrt * ;
254 : gumbel-random-float ( loc scale -- n )
255 random-unit log neg log * - ;
257 : logistic-random-float ( loc scale -- n )
258 random-unit dup 1 swap - / log * + ;
260 : power-random-float ( alpha -- n )
261 [ random-unit log e^ 1 swap - ] dip recip ^ ;
264 { [ os windows? ] [ "random.windows" require ] }
265 { [ os unix? ] [ "random.unix" require ] }
268 "random.mersenne-twister" require