-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.mjs
47 lines (40 loc) · 1.12 KB
/
misc.mjs
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
let rndFn = Math.random;
export function setRandomFunction(fn) {
rndFn = fn;
}
export function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function times(n) {
const arr = new Array(n).fill(true);
return arr.map((_, i) => i);
}
export function randomFromArray(arr) {
const l = arr.length;
const i = Math.floor( l * rndFn() );
return arr[i];
}
function _weightedIndices(weights) {
let indices = [];
weights.forEach((weight, index) => {
const part = times(weight).map((_) => index);
indices = indices.concat(part);
});
return indices;
}
export function randomFromArrayWeighted(arr, weights) {
const arrIndices = _weightedIndices(weights);
const index = randomFromArray(arrIndices);
return arr[index];
}
// lame test
if (false) {
const arr = ['a', 'b', 'c'];
const weights = [6, 2, 1];
console.log(_weightedIndices(weights));
for (const i of times(20)) {
//console.log(`#${i}, ${randomFromArray(arr)}`);
console.log(`#${i}, ${randomFromArrayWeighted(arr, weights)}`);
}
throw new Error('asd');
}