d892824f14246a45508a55b573658955a1b77427
1 // https://stackoverflow.com/a/47593316/12660887
2 function mulberry32(a
) {
4 let t
= a
+= 0x6D2B79F5;
5 t
= Math
.imul(t
^ t
>>> 15, t
| 1);
6 t
^= t
+ Math
.imul(t
^ t
>>> 7, t
| 61);
7 return ((t
^ t
>>> 14) >>> 0) / 4294967296;
11 export const Random
= {
15 setSeed: function(a
) {
16 Random
.rand
= mulberry32(a
);
19 randInt: function(min
, max
) {
25 Random
.setSeed(Math
.floor(Math
.random() * 19840));
26 return Math
.floor(Random
.rand() * (max
- min
)) + min
;
29 randBool: function() {
30 return Random
.randInt(0, 2) == 0;
33 // Inspired by https://github.com/jashkenas/underscore
34 sample: function(arr
, n
) {
36 let cpArr
= arr
.map(e
=> e
);
37 for (let index
= 0; index
< n
; index
++) {
38 const rand
= Random
.randInt(index
, arr
.length
);
39 [ cpArr
[index
], cpArr
[rand
] ] = [ cpArr
[rand
], cpArr
[index
] ];
41 const res
= cpArr
.slice(0, n
);
42 return (n
>= 2 ? res : res
[0]);
45 shuffle: function(arr
) {
46 return Random
.sample(arr
, arr
.length
);