1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.data arrays assocs byte-arrays
4 byte-vectors combinators combinators.short-circuit fry
5 hashtables hashtables.private hash-sets hints io.backend
6 io.binary kernel locals math math.bitwise math.constants
7 math.functions math.order math.ranges namespaces sequences
8 sequences.private sets summary system typed vocabs ;
9 QUALIFIED-WITH: alien.c-types c
10 QUALIFIED-WITH: sets sets
13 SYMBOL: system-random-generator
14 SYMBOL: secure-random-generator
15 SYMBOL: random-generator
17 GENERIC#: seed-random 1 ( obj seed -- obj )
18 GENERIC: random-32* ( obj -- n )
19 GENERIC: random-bytes* ( n obj -- byte-array )
21 M: object random-bytes*
22 [ integer>fixnum-strict [ (byte-array) ] keep ] dip
25 [ random-32* 2over c:int c:set-alien-value ] keep
26 ] while over zero? [ 2drop ] [
27 random-32* c:int <ref> swap head 0 pick copy-unsafe
31 4 swap random-bytes* c:uint deref ;
33 ERROR: no-random-number-generator ;
35 M: no-random-number-generator summary
36 drop "Random number generator is not defined." ;
38 M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
40 M: f random-32* ( obj -- * ) no-random-number-generator ;
43 random-generator get random-32* ;
45 : random-bytes ( n -- byte-array )
46 random-generator get random-bytes* ;
50 :: (random-bits) ( numbits obj -- n )
52 obj random-32* numbits 32 - [ dup 32 > ] [
53 [ 32 shift obj random-32* + ] [ 32 - ] bi*
55 [ shift ] keep obj random-32* swap bits +
58 obj random-32* numbits bits
63 : random-bits ( numbits -- n )
64 random-generator get (random-bits) ;
66 : random-bits* ( numbits -- n )
67 1 - [ random-bits ] keep set-bit ;
71 : next-power-of-2-bits ( m -- numbits )
72 dup 2 <= [ drop 1 ] [ 1 - log2 1 + ] if ; inline
74 :: random-integer-loop ( m obj -- n )
75 obj random-32* 32 m next-power-of-2-bits 32 - [ dup 0 > ] [
76 [ 32 shift obj random-32* + ] [ 32 + ] [ 32 - ] tri*
77 ] while drop [ m * ] [ neg shift ] bi* ; inline
79 GENERIC#: (random-integer) 1 ( m obj -- n )
80 M: fixnum (random-integer) ( m obj -- n ) random-integer-loop ;
81 M: bignum (random-integer) ( m obj -- n ) random-integer-loop ;
83 : random-integer ( m -- n )
84 random-generator get (random-integer) ;
88 GENERIC: random ( obj -- elt )
91 [ f ] [ random-integer ] if-zero ;
95 [ length random-integer ] keep nth
98 M: assoc random >alist random ;
101 dup assoc-size [ drop f ] [
102 [ 0 ] [ array>> ] [ random ] tri* 1 + [
103 [ 2dup array-nth tombstone? [ 2 + ] 2dip ] loop
105 [ array-nth ] [ [ 1 + ] dip array-nth ] 2bi 2array
108 M: sets:set random members random ;
111 dup cardinality [ drop f ] [
112 [ 0 ] [ array>> ] [ random ] tri* 1 + [
113 [ 2dup array-nth tombstone? [ 1 + ] 2dip ] loop
114 ] times [ 1 - ] dip array-nth
117 : randomize-n-last ( seq n -- seq )
118 [ dup length dup ] dip - 1 max '[ dup _ > ]
119 random-generator get '[
120 [ _ (random-integer) ] [ 1 - ] bi
121 [ pick exchange-unsafe ] keep
124 : randomize ( seq -- randomized )
125 dup length randomize-n-last ;
127 ERROR: too-many-samples seq n ;
129 : sample ( seq n -- seq' )
130 2dup [ length ] dip < [ too-many-samples ] when
131 [ [ length <iota> >array ] dip [ randomize-n-last ] keep tail-slice* ]
132 [ drop ] 2bi nths-unsafe ;
134 : delete-random ( seq -- elt )
135 [ length random-integer ] keep
136 [ nth ] 2keep remove-nth! drop ;
138 : with-random ( obj quot -- )
139 random-generator swap with-variable ; inline
141 : with-system-random ( quot -- )
142 system-random-generator get swap with-random ; inline
144 : with-secure-random ( quot -- )
145 secure-random-generator get swap with-random ; inline
149 : (uniform-random-float) ( min max obj -- n )
150 [ random-32* ] keep random-32* [ >float ] bi@
152 [ over - 2.0 -64 ^ * ] dip
157 : uniform-random-float ( min max -- n )
158 random-generator get (uniform-random-float) ; inline
160 M: float random [ f ] [ 0.0 swap uniform-random-float ] if-zero ;
164 : (random-unit) ( obj -- n )
165 [ 0.0 1.0 ] dip (uniform-random-float) ; inline
169 : random-unit ( -- n )
170 random-generator get (random-unit) ; inline
172 : random-units ( length -- sequence )
173 random-generator get '[ _ (random-unit) ] replicate ;
175 : random-integers ( length n -- sequence )
176 random-generator get '[ _ _ (random-integer) ] replicate ;
180 : (cos-random-float) ( -- n )
181 0. 2pi uniform-random-float cos ;
183 : (log-sqrt-random-float) ( -- n )
184 random-unit log -2. * sqrt ;
188 : normal-random-float ( mean sigma -- n )
189 (cos-random-float) (log-sqrt-random-float) * * + ;
191 : lognormal-random-float ( mean sigma -- n )
192 normal-random-float e^ ;
194 : exponential-random-float ( lambda -- n )
195 random-unit log neg swap / ;
197 : weibull-random-float ( alpha beta -- n )
199 [ random-unit log neg ] dip
203 : pareto-random-float ( k alpha -- n )
204 [ random-unit ] dip recip ^ /f ;
208 :: (gamma-random-float>1) ( alpha beta -- n )
209 ! Uses R.C.H. Cheng, "The generation of Gamma
210 ! variables with non-integral shape parameters",
211 ! Applied Statistics, (1977), 26, No. 1, p71-74
212 random-generator get :> rnd
213 2. alpha * 1 - sqrt :> ainv
214 alpha 4. log - :> bbb
217 0 :> r! 0 :> z! 0 :> result! ! initialize locals
220 [ 1. 4.5 log + + z 4.5 * - 0 >= ]
224 rnd (random-unit) :> u1
225 rnd (random-unit) :> u2
227 u1 1. u1 - / log ainv / :> v
235 : (gamma-random-float=1) ( alpha beta -- n )
236 nip random-unit log neg * ;
238 :: (gamma-random-float<1) ( alpha beta -- n )
239 ! Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
240 random-generator get :> rnd
243 0 :> x! 0 :> p! ! initialize locals
246 rnd (random-unit) x alpha 1 - ^ >
248 rnd (random-unit) x neg e^ >
251 rnd (random-unit) b * p!
255 b p - alpha / log neg
257 ] do while x beta * ;
261 : gamma-random-float ( alpha beta -- n )
263 { [ over 1 > ] [ (gamma-random-float>1) ] }
264 { [ over 1 = ] [ (gamma-random-float=1) ] }
265 [ (gamma-random-float<1) ]
268 : beta-random-float ( alpha beta -- n )
269 [ 1. gamma-random-float ] dip over zero?
270 [ 2drop 0 ] [ 1. gamma-random-float dupd + / ] if ;
272 :: von-mises-random-float ( mu kappa -- n )
273 ! Based upon an algorithm published in: Fisher, N.I.,
274 ! "Statistical Analysis of Circular Data", Cambridge
275 ! University Press, 1993.
276 random-generator get :> rnd
278 2pi rnd (random-unit) *
280 4. kappa sq * 1. + sqrt 1. + :> a
281 a 2. a * sqrt - 2. kappa * / :> b
282 b sq 1. + 2. b * / :> r
284 0 :> c! 0 :> _f! ! initialize locals
287 [ 2. c - c * < ] [ 1. c - e^ c * <= ]
290 rnd (random-unit) pi * cos :> z
291 r z * 1. + r z + / _f!
296 rnd (random-unit) 0.5 > [ + ] [ - ] if
301 :: (triangular-random-float) ( low high mode -- n )
302 mode low - high low - / :> c!
305 u c > [ 1. u - u! 1. c - c! swap ] when
306 [ - u c * sqrt * ] keep + ;
310 : triangular-random-float ( low high -- n )
311 2dup + 2 /f (triangular-random-float) ;
313 : laplace-random-float ( mean scale -- n )
314 random-unit dup 0.5 <
315 [ 2 * log ] [ 1 swap - 2 * log neg ] if * + ;
317 : cauchy-random-float ( median scale -- n )
318 random-unit 0.5 - pi * tan * + ;
320 : chi-square-random-float ( dof -- n )
321 [ 0.5 ] dip 2 * gamma-random-float ;
323 : student-t-random-float ( dof -- n )
324 [ 0 1 normal-random-float ] dip
325 [ chi-square-random-float ] [ / ] bi sqrt / ;
327 : inv-gamma-random-float ( shape scale -- n )
328 recip gamma-random-float recip ;
330 : rayleigh-random-float ( mode -- n )
331 random-unit log -2 * sqrt * ;
333 : gumbel-random-float ( loc scale -- n )
334 random-unit log neg log * - ;
336 : logistic-random-float ( loc scale -- n )
337 random-unit dup 1 swap - / log * + ;
339 : power-random-float ( alpha -- n )
340 [ random-unit log e^ 1 swap - ] dip recip ^ ;
343 : poisson-random-float ( mean -- n )
344 [ -1 0 ] dip [ 2dup < ] random-generator get '[
345 [ 1 + ] 2dip [ _ (random-unit) log neg + ] dip
349 { [ os windows? ] [ "random.windows" require ] }
350 { [ os unix? ] [ "random.unix" require ] }
353 "random.mersenne-twister" require