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 fry io.backend io.binary
5 kernel locals math math.bitwise math.constants math.functions
6 math.order math.ranges namespaces sequences sequences.private
7 sets summary system vocabs hints typed ;
10 SYMBOL: system-random-generator
11 SYMBOL: secure-random-generator
12 SYMBOL: random-generator
14 GENERIC# seed-random 1 ( tuple seed -- tuple' )
15 GENERIC: random-32* ( tuple -- r )
16 GENERIC: random-bytes* ( n tuple -- byte-array )
18 M: object random-bytes* ( n tuple -- byte-array )
19 [ [ <byte-vector> ] keep 4 /mod ] dip
20 [ pick '[ _ random-32* int <ref> _ push-all ] times ]
23 [ 2drop ] [ random-32* int <ref> swap head append! ] if
24 ] bi-curry bi* B{ } like ;
26 HINTS: M\ object random-bytes* { fixnum object } ;
28 M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ;
30 ERROR: no-random-number-generator ;
32 M: no-random-number-generator summary
33 drop "Random number generator is not defined." ;
35 M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
37 M: f random-32* ( obj -- * ) no-random-number-generator ;
39 : random-32 ( -- n ) random-generator get random-32* ;
41 TYPED: random-bytes ( n: fixnum -- byte-array: byte-array )
42 random-generator get random-bytes* ; inline
46 : (random-integer) ( bits -- n required-bits )
47 [ random-32 32 ] dip 32 - [ dup 0 > ] [
48 [ 32 shift random-32 + ] [ 32 + ] [ 32 - ] tri*
51 : random-integer ( n -- n' )
52 dup next-power-of-2 log2 (random-integer)
57 : random-bits ( numbits -- r ) 2^ random-integer ;
59 : random-bits* ( numbits -- n )
60 1 - [ random-bits ] keep set-bit ;
62 GENERIC: random ( obj -- elt )
64 M: integer random [ f ] [ random-integer ] if-zero ;
68 [ length random-integer ] keep nth
71 : randomize-n-last ( seq n -- seq )
72 [ dup length dup ] dip - 1 max '[ dup _ > ]
73 [ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
76 : randomize ( seq -- randomized )
77 dup length randomize-n-last ;
79 ERROR: too-many-samples seq n ;
81 : sample ( seq n -- seq' )
82 2dup [ length ] dip < [ too-many-samples ] when
83 [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
86 : delete-random ( seq -- elt )
87 [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
89 : with-random ( tuple quot -- )
90 random-generator swap with-variable ; inline
92 : with-system-random ( quot -- )
93 system-random-generator get swap with-random ; inline
95 : with-secure-random ( quot -- )
96 secure-random-generator get swap with-random ; inline
98 : uniform-random-float ( min max -- n )
99 4 random-bytes uint deref >float
100 4 random-bytes uint deref >float
102 [ over - 2.0 -64 ^ * ] dip
105 : random-unit ( -- n )
106 0.0 1.0 uniform-random-float ; inline
108 : (cos-random-float) ( -- n )
109 0. 2pi uniform-random-float cos ;
111 : (log-sqrt-random-float) ( -- n )
112 random-unit log -2. * sqrt ;
114 : normal-random-float ( mean sigma -- n )
115 (cos-random-float) (log-sqrt-random-float) * * + ;
117 : lognormal-random-float ( mean sigma -- n )
118 normal-random-float exp ;
120 : exponential-random-float ( lambda -- n )
121 random-unit log neg swap / ;
123 : weibull-random-float ( alpha beta -- n )
125 [ random-unit log neg ] dip
129 : pareto-random-float ( alpha -- n )
130 [ random-unit ] dip [ 1. swap / ] bi@ ^ ;
132 : beta-random-float ( alpha beta -- n )
133 [ 1. normal-random-float ] dip over zero?
134 [ 2drop 0 ] [ 1. normal-random-float dupd + / ] if ;
137 { [ os windows? ] [ "random.windows" require ] }
138 { [ os unix? ] [ "random.unix" require ] }
141 "random.mersenne-twister" require