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 ;
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* 4 >le _ push-all ] times ]
23 [ 2drop ] [ random-32* 4 >le swap head append! ] if
26 M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ;
28 ERROR: no-random-number-generator ;
30 M: no-random-number-generator summary
31 drop "Random number generator is not defined." ;
33 M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
35 M: f random-32* ( obj -- * ) no-random-number-generator ;
37 : random-32 ( -- n ) random-generator get random-32* ;
39 : random-bytes ( n -- byte-array )
40 random-generator get random-bytes* ;
44 : (random-integer) ( bits -- n required-bits )
45 [ random-32 32 ] dip 32 - [ dup 0 > ] [
46 [ 32 shift random-32 + ] [ 32 + ] [ 32 - ] tri*
49 : random-integer ( n -- n' )
50 dup next-power-of-2 log2 (random-integer)
55 : random-bits ( numbits -- r ) 2^ random-integer ;
57 : random-bits* ( numbits -- n )
58 1 - [ random-bits ] keep set-bit ;
60 GENERIC: random ( obj -- elt )
62 M: integer random [ f ] [ random-integer ] if-zero ;
66 [ length random-integer ] keep nth
69 : randomize-n-last ( seq n -- seq )
70 [ dup length dup ] dip - 1 max '[ dup _ > ]
71 [ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
74 : randomize ( seq -- randomized )
75 dup length randomize-n-last ;
77 ERROR: too-many-samples seq n ;
79 : sample ( seq n -- seq' )
80 2dup [ length ] dip < [ too-many-samples ] when
81 [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
84 : delete-random ( seq -- elt )
85 [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
87 : with-random ( tuple quot -- )
88 random-generator swap with-variable ; inline
90 : with-system-random ( quot -- )
91 system-random-generator get swap with-random ; inline
93 : with-secure-random ( quot -- )
94 secure-random-generator get swap with-random ; inline
96 : uniform-random-float ( min max -- n )
97 4 random-bytes underlying>> uint deref >float
98 4 random-bytes underlying>> uint deref >float
100 [ over - 2.0 -64 ^ * ] dip
103 : (cos-random-float) ( -- n )
104 0. 2. pi * uniform-random-float cos ;
106 : (log-sqrt-random-float) ( -- n )
107 0. 1. uniform-random-float log -2. * sqrt ;
109 : normal-random-float ( mean sigma -- n )
110 (cos-random-float) (log-sqrt-random-float) * * + ;
113 { [ os windows? ] [ "random.windows" require ] }
114 { [ os unix? ] [ "random.unix" require ] }
117 "random.mersenne-twister" require