]> gitweb.factorcode.org Git - factor.git/blob - basis/random/random.factor
Remove many uses of <int> and *int etc
[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 arrays assocs byte-arrays
4 byte-vectors combinators fry io.backend io.binary kernel locals
5 math math.bitwise math.constants math.functions math.order
6 math.ranges namespaces sequences sets summary system
7 vocabs.loader ;
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* le> ;
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-bytes ( n -- byte-array )
38     random-generator get random-bytes* ;
39
40 <PRIVATE
41
42 : random-integer ( n -- n' )
43     dup log2 7 + 8 /i 1 +
44     [ random-bytes >byte-array byte-array>bignum ]
45     [ 3 shift 2^ ] bi / * >integer ;
46
47 PRIVATE>
48
49 : random-bits ( numbits -- r ) 2^ random-integer ;
50
51 : random-bits* ( numbits -- n )
52     1 - [ random-bits ] keep set-bit ;
53
54 GENERIC: random ( obj -- elt )
55
56 M: integer random [ f ] [ random-integer ] if-zero ;
57
58 M: sequence random
59     [ f ] [
60         [ length random-integer ] keep nth
61     ] if-empty ;
62
63 : random-32 ( -- n ) random-generator get random-32* ;
64
65 : randomize-n-last ( seq n -- seq ) 
66     [ dup length dup ] dip - 1 max '[ dup _ > ] 
67     [ [ random ] [ 1 - ] bi [ pick exchange ] keep ]
68     while drop ;
69
70 : randomize ( seq -- randomized )
71     dup length randomize-n-last ;
72
73 ERROR: too-many-samples seq n ;
74
75 : sample ( seq n -- seq' )
76     2dup [ length ] dip < [ too-many-samples ] when
77     [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
78     [ drop ] 2bi nths ;
79
80 : delete-random ( seq -- elt )
81     [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
82
83 : with-random ( tuple quot -- )
84     random-generator swap with-variable ; inline
85
86 : with-system-random ( quot -- )
87     system-random-generator get swap with-random ; inline
88
89 : with-secure-random ( quot -- )
90     secure-random-generator get swap with-random ; inline
91
92 : uniform-random-float ( min max -- n )
93     4 random-bytes underlying>> uint deref >float
94     4 random-bytes underlying>> uint deref >float
95     2.0 32 ^ * +
96     [ over - 2.0 -64 ^ * ] dip
97     * + ; inline
98
99 : normal-random-float ( mean sigma -- n )
100     0.0 1.0 uniform-random-float
101     0.0 1.0 uniform-random-float
102     [ 2 pi * * cos ]
103     [ 1.0 swap - log -2.0 * sqrt ]
104     bi* * * + ;
105
106 {
107     { [ os windows? ] [ "random.windows" require ] }
108     { [ os unix? ] [ "random.unix" require ] }
109 } cond
110
111 "random.mersenne-twister" require