]> gitweb.factorcode.org Git - factor.git/blob - basis/random/random.factor
random: adding lognormal, exponential, weibull, pareto, gauss, and beta distributions.
[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 hints typed ;
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* int <ref> _ push-all ] times ]
21     [
22         over zero?
23         [ 2drop ] [ random-32* int <ref> swap head append! ] if
24     ] bi-curry bi* B{ } like ;
25
26 HINTS: M\ object random-bytes* { fixnum object } ;
27
28 M: object random-32* ( tuple -- r ) 4 swap random-bytes* be> ;
29
30 ERROR: no-random-number-generator ;
31
32 M: no-random-number-generator summary
33     drop "Random number generator is not defined." ;
34
35 M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
36
37 M: f random-32* ( obj -- * ) no-random-number-generator ;
38
39 : random-32 ( -- n ) random-generator get random-32* ;
40
41 TYPED: random-bytes ( n: fixnum -- byte-array: byte-array )
42     random-generator get random-bytes* ; inline
43
44 <PRIVATE
45
46 : (random-integer) ( bits -- n required-bits )
47     [ random-32 32 ] dip 32 - [ dup 0 > ] [
48         [ 32 shift random-32 + ] [ 32 + ] [ 32 - ] tri*
49     ] while drop ;
50
51 : random-integer ( n -- n' )
52     dup next-power-of-2 log2 (random-integer)
53     [ * ] [ 2^ /i ] bi* ;
54
55 PRIVATE>
56
57 : random-bits ( numbits -- r ) 2^ random-integer ;
58
59 : random-bits* ( numbits -- n )
60     1 - [ random-bits ] keep set-bit ;
61
62 GENERIC: random ( obj -- elt )
63
64 M: integer random [ f ] [ random-integer ] if-zero ;
65
66 M: sequence random
67     [ f ] [
68         [ length random-integer ] keep nth
69     ] if-empty ;
70
71 : randomize-n-last ( seq n -- seq )
72     [ dup length dup ] dip - 1 max '[ dup _ > ]
73     [ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
74     while drop ;
75
76 : randomize ( seq -- randomized )
77     dup length randomize-n-last ;
78
79 ERROR: too-many-samples seq n ;
80
81 : sample ( seq n -- seq' )
82     2dup [ length ] dip < [ too-many-samples ] when
83     [ [ length iota >array ] dip [ randomize-n-last ] keep tail-slice* ]
84     [ drop ] 2bi nths ;
85
86 : delete-random ( seq -- elt )
87     [ length random-integer ] keep [ nth ] 2keep remove-nth! drop ;
88
89 : with-random ( tuple quot -- )
90     random-generator swap with-variable ; inline
91
92 : with-system-random ( quot -- )
93     system-random-generator get swap with-random ; inline
94
95 : with-secure-random ( quot -- )
96     secure-random-generator get swap with-random ; inline
97
98 : uniform-random-float ( min max -- n )
99     4 random-bytes uint deref >float
100     4 random-bytes uint deref >float
101     2.0 32 ^ * +
102     [ over - 2.0 -64 ^ * ] dip
103     * + ; inline
104
105 : (cos-random-float) ( -- n )
106     0. 2. pi * uniform-random-float cos ;
107
108 : (log-sqrt-random-float) ( -- n )
109     0. 1. uniform-random-float log -2. * sqrt ;
110
111 : normal-random-float ( mean sigma -- n )
112     (cos-random-float) (log-sqrt-random-float) * * + ;
113
114 {
115     { [ os windows? ] [ "random.windows" require ] }
116     { [ os unix? ] [ "random.unix" require ] }
117 } cond
118
119 : lognormal-random-float ( mean sigma -- n )
120     normal-random-float exp ;
121
122 : exponential-random-float ( lambda -- n )
123     0. 1. uniform-random-float 1 swap - log neg swap / ;
124
125 : weibull-random-float ( lambda k -- n )
126     [ 0. 1. uniform-random-float 1 swap - log neg ] dip 1. swap / ^ * ;
127
128 : pareto-random-float ( alpha -- n )
129     [ 0. 1. uniform-random-float 1 swap - ] dip [ 1. swap / ] bi@ ^ ;
130
131 : gauss-random-float ( mean sigma -- n )
132     0. 1. uniform-random-float 1 swap - log -2 * sqrt
133     (cos-random-float) * * + ;
134
135 : beta-random-float ( alpha beta -- n )
136     [ 1. gauss-random-float ] dip over zero?
137     [ 2drop 0 ] [ 1. gauss-random-float dupd + / ] if ;
138
139 "random.mersenne-twister" require