]> gitweb.factorcode.org Git - factor.git/blob - basis/random/random.factor
Make "foo.private" require load foo instead.
[factor.git] / basis / random / random.factor
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 ;
8 IN: random
9
10 SYMBOL: system-random-generator
11 SYMBOL: secure-random-generator
12 SYMBOL: random-generator
13
14 GENERIC# seed-random 1 ( tuple seed -- tuple' )
15 GENERIC: random-32* ( tuple -- r )
16 GENERIC: random-bytes* ( n tuple -- byte-array )
17
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 ]
21     [
22         over zero?
23         [ 2drop ] [ random-32* 4 >le swap head append! ] if
24     ] bi-curry bi* ;
25
26 M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ;
27
28 ERROR: no-random-number-generator ;
29
30 M: no-random-number-generator summary
31     drop "Random number generator is not defined." ;
32
33 M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
34
35 M: f random-32* ( obj -- * ) no-random-number-generator ;
36
37 : random-32 ( -- n ) random-generator get random-32* ;
38
39 : random-bytes ( n -- byte-array )
40     random-generator get random-bytes* ;
41
42 <PRIVATE
43
44 : (random-integer) ( bits -- n required-bits )
45     [ random-32 32 ] dip 32 - [ dup 0 > ] [
46         [ 32 shift random-32 + ] [ 32 + ] [ 32 - ] tri*
47     ] while drop ;
48
49 : random-integer ( n -- n' )
50     dup next-power-of-2 log2 (random-integer)
51     [ * ] [ 2^ /i ] bi* ;
52
53 PRIVATE>
54
55 : random-bits ( numbits -- r ) 2^ random-integer ;
56
57 : random-bits* ( numbits -- n )
58     1 - [ random-bits ] keep set-bit ;
59
60 GENERIC: random ( obj -- elt )
61
62 M: integer random [ f ] [ random-integer ] if-zero ;
63
64 M: sequence random
65     [ f ] [
66         [ length random-integer ] keep nth
67     ] if-empty ;
68
69 : randomize-n-last ( seq n -- seq )
70     [ dup length dup ] dip - 1 max '[ dup _ > ]
71     [ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
72     while drop ;
73
74 : randomize ( seq -- randomized )
75     dup length randomize-n-last ;
76
77 ERROR: too-many-samples seq n ;
78
79 : sample ( seq n -- seq' )
80     2dup [ length ] dip < [ too-many-samples ] when
81     [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
82     [ drop ] 2bi nths ;
83
84 : delete-random ( seq -- elt )
85     [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
86
87 : with-random ( tuple quot -- )
88     random-generator swap with-variable ; inline
89
90 : with-system-random ( quot -- )
91     system-random-generator get swap with-random ; inline
92
93 : with-secure-random ( quot -- )
94     secure-random-generator get swap with-random ; inline
95
96 : uniform-random-float ( min max -- n )
97     4 random-bytes underlying>> uint deref >float
98     4 random-bytes underlying>> uint deref >float
99     2.0 32 ^ * +
100     [ over - 2.0 -64 ^ * ] dip
101     * + ; inline
102
103 : (cos-random-float) ( -- n )
104     0. 2. pi * uniform-random-float cos ;
105
106 : (log-sqrt-random-float) ( -- n )
107     0. 1. uniform-random-float log -2. * sqrt ;
108
109 : normal-random-float ( mean sigma -- n )
110     (cos-random-float) (log-sqrt-random-float) * * + ;
111
112 {
113     { [ os windows? ] [ "random.windows" require ] }
114     { [ os unix? ] [ "random.unix" require ] }
115 } cond
116
117 "random.mersenne-twister" require