-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pickRandom.test.js
401 lines (298 loc) · 15.6 KB
/
pickRandom.test.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import assert from 'assert'
import { filter, times } from 'lodash'
import math from '../../../../src/bundleAny'
import { flatten } from '../../../../src/utils/array'
const math2 = math.create({ randomSeed: 'test2' })
const pickRandom = math2.pickRandom
describe('pickRandom', function () {
it('should have a function pickRandom', function () {
assert.strictEqual(typeof math.pickRandom, 'function')
})
it('should throw an error if the length of the weights does not match the length of the possibles', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4]
const number = 2
assert.throws(function () {
pickRandom(possibles, weights)
}, /Weights must have the same length as possibles/)
assert.throws(function () {
pickRandom(possibles, number, weights)
}, /Weights must have the same length as possibles/)
assert.throws(function () {
pickRandom(possibles, weights, number)
}, /Weights must have the same length as possibles/)
})
it('should throw an error if the weights array contains a non number or negative value', function () {
const possibles = [11, 22, 33, 44, 55]
let weights = [1, 5, 2, -1, 6]
assert.throws(function () {
pickRandom(possibles, weights)
}, /Weights must be an array of positive numbers/)
weights = [1, 5, 2, 'stinky', 6]
assert.throws(function () {
pickRandom(possibles, weights)
}, /Weights must be an array of positive numbers/)
})
it('should return a single value if no number argument was passed', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4, 6]
assert.notStrictEqual(possibles.indexOf(pickRandom(possibles)), -1)
assert.notStrictEqual(possibles.indexOf(pickRandom(possibles, weights)), -1)
})
it('should return a single value if no number argument was passed (2)', function () {
const possibles = [5]
assert.strictEqual(pickRandom(possibles), 5)
})
it('should return the given array if the given number is equal its length', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4, 6]
const number = 5
pickRandom(possibles, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, number, weights).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, weights, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
})
it('should return the given array if the given number is greater than its length', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4, 6]
const number = 6
pickRandom(possibles, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, number, weights).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, weights, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
})
it('should return an empty array if the given number is 0', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4, 6]
const number = 0
assert.strictEqual(pickRandom(possibles, number).length, 0)
assert.strictEqual(pickRandom(possibles, number, weights).length, 0)
assert.strictEqual(pickRandom(possibles, weights, number).length, 0)
})
it('should return an array of length 1 if the number passed is 1', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4, 6]
const number = 1
assert(Array.isArray(pickRandom(possibles, number)))
assert(Array.isArray(pickRandom(possibles, number, weights)))
assert(Array.isArray(pickRandom(possibles, weights, number)))
assert.strictEqual(pickRandom(possibles, number).length, 1)
assert.strictEqual(pickRandom(possibles, number, weights).length, 1)
assert.strictEqual(pickRandom(possibles, weights, number).length, 1)
})
it('should pick the given number of values from the given array', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4, 6]
const number = 3
assert.strictEqual(pickRandom(possibles, number).length, number)
assert.strictEqual(pickRandom(possibles, number, weights).length, number)
assert.strictEqual(pickRandom(possibles, weights, number).length, number)
})
it('should pick a number from the given multi dimensional array following an uniform distribution', function () {
const possibles = [[11, 12], [22, 23], [33, 34], [44, 45], [55, 56]]
const picked = []
times(1000, () => picked.push(pickRandom(possibles)))
flatten(possibles).forEach(possible => {
const count = filter(flatten(picked), val => val === possible).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
})
})
it('should pick a value from the given multi dimensional array following an uniform distribution', function () {
// just to be sure that works for any kind of array
const possibles = [[[11], [12]], ['test', 45], 'another test', 10, false, [1.3, 4.5, true]]
const picked = []
times(1000, () => picked.push(pickRandom(possibles)))
flatten(possibles).forEach(possible => {
const count = filter(picked, val => val === possible).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
})
})
it('should pick a value from the given array following an uniform distribution if only possibles are passed', function () {
const possibles = [11, 22, 33, 44, 55]
const picked = []
let count
times(1000, function () {
picked.push(pickRandom(possibles))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
})
it('should pick a value from the given matrix following an uniform distribution', function () {
const possibles = math.matrix([11, 22, 33, 44, 55])
const picked = []
let count
times(1000, function () {
picked.push(pickRandom(possibles))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
})
it('should pick a given number of values from the given array following an uniform distribution if no weights were passed', function () {
const possibles = [11, 22, 33, 44, 55]
const number = 2
const picked = []
let count
times(1000, function () {
picked.push.apply(picked, pickRandom(possibles, number))
})
assert.strictEqual(picked.length, 2000)
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
})
it('should pick numbers from the given matrix following an uniform distribution', function () {
const possibles = math.matrix([11, 22, 33, 44, 55])
const number = 3
const picked = []
let count
times(1000, function () {
picked.push.apply(picked, pickRandom(possibles, number))
})
assert.strictEqual(picked.length, 3000)
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
})
it('should pick a value from the given array following a weighted distribution', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 4, 0, 2, 3]
const picked = []
let count
times(1000, function () {
picked.push(pickRandom(possibles, weights))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
})
it('should pick a value from the given matrix following a weighted distribution', function () {
const possibles = math.matrix([11, 22, 33, 44, 55])
const weights = [1, 4, 0, 2, 3]
const picked = []
let count
times(1000, function () {
picked.push(pickRandom(possibles, weights))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
})
it('should return an array of values from the given array following a weighted distribution', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 4, 0, 2, 3]
const number = 2
const picked = []
let count
times(1000, function () {
picked.push.apply(picked, pickRandom(possibles, number, weights))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
times(1000, function () {
picked.push.apply(picked, pickRandom(possibles, weights, number))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
})
it('should return an array of values from the given matrix following a weighted distribution', function () {
const possibles = math.matrix([11, 22, 33, 44, 55])
const weights = [1, 4, 0, 2, 3]
const number = 2
const picked = []
let count
times(1000, function () {
picked.push.apply(picked, pickRandom(possibles, number, weights))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
times(1000, function () {
picked.push.apply(picked, pickRandom(possibles, weights, number))
})
count = filter(picked, function (val) { return val === 11 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
count = filter(picked, function (val) { return val === 22 }).length
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
count = filter(picked, function (val) { return val === 33 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0)
count = filter(picked, function (val) { return val === 44 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
count = filter(picked, function (val) { return val === 55 }).length
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
})
it('should throw an error in case of wrong type of arguments', function () {
assert.throws(function () { pickRandom(23) }, /Unexpected type of argument/)
assert.throws(function () { pickRandom() }, /Too few arguments/)
assert.throws(function () { pickRandom([], 23, [], 9) }, /Too many arguments/)
// TODO: more type testing...
})
it('should LaTeX pickRandom', function () {
const expression = math.parse('pickRandom([1,2,3])')
assert.strictEqual(expression.toTex(), '\\mathrm{pickRandom}\\left(\\begin{bmatrix}1\\\\2\\\\3\\\\\\end{bmatrix}\\right)')
})
})