]> gitweb.factorcode.org Git - factor.git/blob - basis/random/random.factor
random is generic now -- iota random -> random
[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 assocs byte-arrays byte-vectors
4 combinators fry io.backend io.binary kernel locals math
5 math.bitwise math.constants math.functions math.ranges
6 namespaces sequences sets summary system vocabs.loader ;
7 IN: random
8
9 SYMBOL: system-random-generator
10 SYMBOL: secure-random-generator
11 SYMBOL: random-generator
12
13 GENERIC# seed-random 1 ( tuple seed -- tuple' )
14 GENERIC: random-32* ( tuple -- r )
15 GENERIC: random-bytes* ( n tuple -- byte-array )
16
17 M: object random-bytes* ( n tuple -- byte-array )
18     [ [ <byte-vector> ] keep 4 /mod ] dip
19     [ pick '[ _ random-32* 4 >le _ push-all ] times ]
20     [
21         over zero?
22         [ 2drop ] [ random-32* 4 >le swap head append! ] if
23     ] bi-curry bi* ;
24
25 M: object random-32* ( tuple -- r ) 4 swap random-bytes* le> ;
26
27 ERROR: no-random-number-generator ;
28
29 M: no-random-number-generator summary
30     drop "Random number generator is not defined." ;
31
32 M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
33
34 M: f random-32* ( obj -- * ) no-random-number-generator ;
35
36 : random-bytes ( n -- byte-array )
37     random-generator get random-bytes* ;
38
39 <PRIVATE
40
41 : random-integer ( n -- n' )
42     dup log2 7 + 8 /i 1 +
43     [ random-bytes >byte-array byte-array>bignum ]
44     [ 3 shift 2^ ] bi / * >integer ;
45
46 PRIVATE>
47
48 : random-bits ( numbits -- r ) 2^ random-integer ;
49
50 : random-bits* ( numbits -- n )
51     1 - [ random-bits ] keep set-bit ;
52
53 GENERIC: random ( obj -- elt )
54
55 M: integer random [ f ] [ random-integer ] if-zero ;
56
57 M: sequence random
58     [ f ] [
59         [ length random-integer ] keep nth
60     ] if-empty ;
61
62 : random-32 ( -- n ) random-generator get random-32* ;
63
64 : randomize ( seq -- seq )
65     dup length [ dup 1 > ]
66     [ [ random ] [ 1 - ] bi [ pick exchange ] keep ]
67     while drop ;
68
69 ERROR: too-many-samples seq n ;
70
71 <PRIVATE
72
73 :: next-sample ( length n seq hashtable -- elt )
74     n hashtable key? [
75         length n 1 + length mod seq hashtable next-sample
76     ] [
77         n hashtable conjoin
78         n seq nth
79     ] if ;
80
81 PRIVATE>
82
83 : sample ( seq n -- seq' )
84     2dup [ length ] dip < [ too-many-samples ] when
85     swap [ length ] [ ] bi H{ } clone 
86     '[ _ dup random _ _ next-sample ] replicate ;
87
88 : delete-random ( seq -- elt )
89     [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
90
91 : with-random ( tuple quot -- )
92     random-generator swap with-variable ; inline
93
94 : with-system-random ( quot -- )
95     system-random-generator get swap with-random ; inline
96
97 : with-secure-random ( quot -- )
98     secure-random-generator get swap with-random ; inline
99
100 : uniform-random-float ( min max -- n )
101     4 random-bytes underlying>> *uint >float
102     4 random-bytes underlying>> *uint >float
103     2.0 32 ^ * +
104     [ over - 2.0 -64 ^ * ] dip
105     * + ; inline
106
107 : normal-random-float ( mean sigma -- n )
108     0.0 1.0 uniform-random-float
109     0.0 1.0 uniform-random-float
110     [ 2 pi * * cos ]
111     [ 1.0 swap - log -2.0 * sqrt ]
112     bi* * * + ;
113
114 {
115     { [ os windows? ] [ "random.windows" require ] }
116     { [ os unix? ] [ "random.unix" require ] }
117 } cond
118
119 "random.mersenne-twister" require