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 ( tuple seed -- tuple' )
18 GENERIC: random-32* ( tuple -- r )
19 GENERIC: random-bytes* ( n tuple -- byte-array )
21 M: object random-bytes* ( n tuple -- byte-array )
22 [ [ <byte-vector> ] keep 4 /mod ] dip
23 [ pick '[ _ random-32* c:int <ref> _ push-all ] times ]
26 [ 2drop ] [ random-32* c:int <ref> swap head append! ] if
27 ] bi-curry bi* B{ } like ;
29 HINTS: M\ object random-bytes* { fixnum object } ;
31 M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ;
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 ;
42 : random-32 ( -- n ) random-generator get random-32* ;
44 TYPED: random-bytes ( n: fixnum -- byte-array: byte-array )
45 random-generator get random-bytes* ; inline
49 :: ((random-integer)) ( bits obj -- n required-bits )
50 obj random-32* 32 bits 32 - [ dup 0 > ] [
51 [ 32 shift obj random-32* + ] [ 32 + ] [ 32 - ] tri*
54 : (random-integer) ( n obj -- n' )
55 [ dup next-power-of-2 log2 ] dip ((random-integer))
58 : random-integer ( n -- n' )
59 random-generator get (random-integer) ;
63 : random-bits ( numbits -- r ) 2^ random-integer ;
65 : random-bits* ( numbits -- n )
66 1 - [ random-bits ] keep set-bit ;
68 GENERIC: random ( obj -- elt )
70 M: integer random [ f ] [ random-integer ] if-zero ;
74 [ length random-integer ] keep nth
77 M: assoc random >alist random ;
80 dup assoc-size [ drop f ] [
81 [ 0 ] [ array>> ] [ random ] tri* 1 + [
82 [ 2dup array-nth tombstone? [ 2 + ] 2dip ] loop
84 [ array-nth ] [ [ 1 + ] dip array-nth ] 2bi 2array
87 M: sets:set random members random ;
89 M: hash-set random table>> random first ;
91 : randomize-n-last ( seq n -- seq )
92 [ dup length dup ] dip - 1 max '[ dup _ > ]
93 [ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
96 : randomize ( seq -- randomized )
97 dup length randomize-n-last ;
99 ERROR: too-many-samples seq n ;
101 : sample ( seq n -- seq' )
102 2dup [ length ] dip < [ too-many-samples ] when
103 [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
106 : delete-random ( seq -- elt )
107 [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
109 : with-random ( tuple quot -- )
110 random-generator swap with-variable ; inline
112 : with-system-random ( quot -- )
113 system-random-generator get swap with-random ; inline
115 : with-secure-random ( quot -- )
116 secure-random-generator get swap with-random ; inline
120 : (uniform-random-float) ( min max obj -- n )
121 [ 4 4 ] dip [ random-bytes* c:uint deref >float ] curry bi@
123 [ over - 2.0 -64 ^ * ] dip
128 : uniform-random-float ( min max -- n )
129 random-generator get (uniform-random-float) ; inline
131 M: float random [ f ] [ 0.0 swap uniform-random-float ] if-zero ;
133 : random-unit ( -- n )
134 0.0 1.0 uniform-random-float ; inline
136 : random-units ( length -- sequence )
137 random-generator get '[ 0.0 1.0 _ (uniform-random-float) ] replicate ;
139 : random-integers ( length n -- sequence )
140 random-generator get '[ _ _ (random-integer) ] replicate ;
142 : (cos-random-float) ( -- n )
143 0. 2pi uniform-random-float cos ;
145 : (log-sqrt-random-float) ( -- n )
146 random-unit log -2. * sqrt ;
148 : normal-random-float ( mean sigma -- n )
149 (cos-random-float) (log-sqrt-random-float) * * + ;
151 : lognormal-random-float ( mean sigma -- n )
152 normal-random-float e^ ;
154 : exponential-random-float ( lambda -- n )
155 random-unit log neg swap / ;
157 : weibull-random-float ( alpha beta -- n )
159 [ random-unit log neg ] dip
163 : pareto-random-float ( k alpha -- n )
164 [ random-unit ] dip recip ^ /f ;
166 :: (gamma-random-float>1) ( alpha beta -- n )
167 ! Uses R.C.H. Cheng, "The generation of Gamma
168 ! variables with non-integral shape parameters",
169 ! Applied Statistics, (1977), 26, No. 1, p71-74
170 2. alpha * 1 - sqrt :> ainv
171 alpha 4. log - :> bbb
174 0 :> r! 0 :> z! 0 :> result! ! initialize locals
177 [ 1. 4.5 log + + z 4.5 * - 0 >= ]
184 u1 1. u1 - / log ainv / :> v
192 : (gamma-random-float=1) ( alpha beta -- n )
193 nip random-unit log neg * ;
195 :: (gamma-random-float<1) ( alpha beta -- n )
196 ! Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
199 0 :> x! 0 :> p! ! initialize locals
202 random-unit x alpha 1 - ^ >
204 random-unit x neg e^ >
211 b p - alpha / log neg
213 ] do while x beta * ;
215 : gamma-random-float ( alpha beta -- n )
217 { [ over 1 > ] [ (gamma-random-float>1) ] }
218 { [ over 1 = ] [ (gamma-random-float=1) ] }
219 [ (gamma-random-float<1) ]
222 : beta-random-float ( alpha beta -- n )
223 [ 1. gamma-random-float ] dip over zero?
224 [ 2drop 0 ] [ 1. gamma-random-float dupd + / ] if ;
226 :: von-mises-random-float ( mu kappa -- n )
227 ! Based upon an algorithm published in: Fisher, N.I.,
228 ! "Statistical Analysis of Circular Data", Cambridge
229 ! University Press, 1993.
233 4. kappa sq * 1. + sqrt 1. + :> a
234 a 2. a * sqrt - 2. kappa * / :> b
235 b sq 1. + 2. b * / :> r
237 0 :> c! 0 :> _f! ! initialize locals
240 [ 2. c - c * < ] [ 1. c - e^ c * <= ]
243 random-unit pi * cos :> z
244 r z * 1. + r z + / _f!
248 mu 2pi mod _f cos random-unit 0.5 > [ + ] [ - ] if
251 :: (triangular-random-float) ( low high mode -- n )
252 mode low - high low - / :> c!
255 u c > [ 1. u - u! 1. c - c! swap ] when
256 [ - u c * sqrt * ] keep + ;
258 : triangular-random-float ( low high -- n )
259 2dup + 2 /f (triangular-random-float) ;
261 : laplace-random-float ( mean scale -- n )
262 random-unit dup 0.5 <
263 [ 2 * log ] [ 1 swap - 2 * log neg ] if * + ;
265 : cauchy-random-float ( median scale -- n )
266 random-unit 0.5 - pi * tan * + ;
268 : chi-square-random-float ( dof -- n )
269 [ 0.5 ] dip 2 * gamma-random-float ;
271 : student-t-random-float ( dof -- n )
272 [ 0 1 normal-random-float ] dip
273 [ chi-square-random-float ] [ / ] bi sqrt / ;
275 : inv-gamma-random-float ( shape scale -- n )
276 recip gamma-random-float recip ;
278 : rayleigh-random-float ( mode -- n )
279 random-unit log -2 * sqrt * ;
281 : gumbel-random-float ( loc scale -- n )
282 random-unit log neg log * - ;
284 : logistic-random-float ( loc scale -- n )
285 random-unit dup 1 swap - / log * + ;
287 : power-random-float ( alpha -- n )
288 [ random-unit log e^ 1 swap - ] dip recip ^ ;
291 : poisson-random-float ( mean -- n )
294 [ [ 1 + ] 2dip [ random-unit log neg + ] dip ] while 2drop ;
297 { [ os windows? ] [ "random.windows" require ] }
298 { [ os unix? ] [ "random.unix" require ] }
301 "random.mersenne-twister" require