/
random.clj
211 lines (175 loc) · 4.69 KB
/
random.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
(ns com.yetanalytics.datasim.random
"Seeded random functions"
(:require [com.yetanalytics.datasim.util.maths :as maths]
[clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as sgen])
(:import [java.util
UUID Random]))
(s/def ::seed int?)
(s/def ::rng
(s/with-gen #(instance? Random %)
(fn []
(sgen/fmap (fn [s]
(Random. s))
(s/gen ::seed)))))
(s/fdef seed-rng
:args (s/cat :seed ::seed)
:ret ::rng)
(defn seed-rng
^Random [^Long seed]
(Random. seed))
(s/fdef rand*
:args (s/cat :rng ::rng
:n (s/? number?))
:ret double?)
(defn rand*
(^Double [^Random rng]
(.nextDouble rng))
(^Double [^Random rng n]
(* n (rand* rng))))
(s/fdef rand-int*
:args (s/cat :rng ::rng
:n (s/int-in Integer/MIN_VALUE Integer/MAX_VALUE))
:ret number?)
(defn rand-int*
[rng n]
(int (rand* rng n)))
(s/fdef rand-nth*
:args (s/cat :rng ::rng
:coll (s/every any?
:min-count 1))
:ret any?)
(defn rand-nth*
[rng coll]
(nth coll (rand-int* rng (count coll))))
(s/fdef shuffle*
:args (s/cat :rng ::rng
:coll (s/every any?))
:ret coll?
:fn (fn [{{:keys [rng coll cnt]} :args ret :ret}]
(= (set coll)
(set ret))))
(defn shuffle*
([rng coll]
(shuffle* rng coll (count coll)))
([rng coll cnt]
(lazy-seq
(when (< 0 cnt)
(let [[head [x & tail]] (split-at
(rand-int*
rng
cnt)
coll)]
(cons x
(shuffle*
rng
(concat head tail)
(dec cnt))))))))
(s/fdef random-sample*
:args (s/cat :rng ::rng
:prob (s/double-in :min 0.0 :max 1.0 :NaN? false)
:coll (s/every any? :min-count 1)
:weights (s/?
(s/map-of any? double?)))
:ret coll?)
(defn random-sample*
([rng prob]
(filter (fn [_] (< (rand* rng) prob))))
([rng prob coll]
(filter (fn [_] (< (rand* rng) prob)) coll))
([rng prob coll weights]
(filter (fn [el]
(< (rand* rng)
(maths/min-max 0.0
(+ prob
(get weights el 0.0))
1.0)))
coll)))
(s/fdef rand-gauss
:args (s/cat :rng ::rng
:mean double?
:sd double?)
:ret double?)
(defn rand-gauss
[^Random rng mean sd]
(+ mean
(* sd (.nextGaussian rng))))
(s/fdef rand-long
:args (s/cat :rng ::rng)
:ret int?)
(defn rand-long
[^Random rng]
(.nextLong rng))
(s/fdef rand-uuid
:args (s/cat :rng ::rng)
:ret string?)
(defn rand-uuid
"Produce a random uuid (as a string) for the rng.
Derived from `clojure.test.check.generators/uuid`"
[^Random rng]
(let [x1 (-> (rand-long rng)
(bit-and -45057)
(bit-or 0x4000))
x2 (-> (rand-long rng)
(bit-or -9223372036854775808)
(bit-and -4611686018427387905))]
(.toString (UUID. x1 x2))))
(s/def ::sd
(s/double-in :min 0.0 :infinite? false :NaN? false))
(s/def ::weight
(s/double-in -1.0 1.0))
(s/fdef choose
:args (s/cat :rng ::rng
:weights (s/map-of any? (s/keys :req-un [::weight]))
:coll (s/every any? :min-count 1)
:options (s/keys*
:opt-un [::sd])))
(defn choose
[rng weights coll
& {:keys [sd]
:or {sd 0.25}}]
(let [even-odds (/ 1 (count coll))]
(apply max-key
(fn [el]
(let [weight (get-in weights [el :weight] 0.0)]
(if (<= weight -1.0)
-1.0
(rand-gauss
rng
(+ even-odds (* sd weight))
sd))))
coll)))
(comment
#_(use '(incanter core stats charts io))
(let [rng (seed-rng 42)
runs 10000]
(reduce-kv
(fn [m k v]
(assoc m k (double (/ v runs))))
{}
(frequencies
(repeatedly runs
#(choose rng
{:a 1.0 :b -1.0}
[:a :b :c :d :e :f])))))
(let [rng (seed-rng 42)
runs 1000]
(apply max
(repeatedly runs
#(rand-gauss
rng
(/ 1 1000)
(/ 1 1000)))))
#_(let [rng (seed-rng 42)
v [0 1 2 3 4]
vc (count v)
runs (int (Math/pow vc vc))
ret
(frequencies
(repeatedly runs
#(shuffle* rng
v
)))]
(view (histogram (vals ret)))
)
)