-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandomUtil.js
60 lines (55 loc) · 1.43 KB
/
RandomUtil.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
var RandomUtil = {};
RandomUtil.exponential = function(e)
{
var p = Math.random();
var h = p < .5;
var p = Math.pow(h ? p * 2 : (1 - p) * 2, e);
p = h ? p / 2 : 1 - (p / 2);
return p;
}
RandomUtil.pick = function(pool, exceptions)
{
if (exceptions != null)
{
var pool2 = [];
var n = pool.length;
for (var i = 0; i < n; i++)
{
var item = pool[i];
if (exceptions.indexOf(item) == -1) pool2.push(item);
}
pool = pool2;
}
return pool[Math.floor(Math.random() * pool.length)];
}
RandomUtil.between = function(min, max, integer, extremeFactor)
{
var p = Math.random();
if (extremeFactor)
{
var f = Math.pow((p < .5) ? p * 2 : (1 - p) * 2, extremeFactor);
p = (p < .5) ? f / 2 : 1 - (f / 2);
}
var n = min + p * (max-min);
if (integer) return Math.floor(n);
else return n;
}
/**
* Picks a random index/value from array using weights
* @param weights Array with numerical values for weights, e.g: [3, 5.4, 7.454, 2.1, 0]
* @param values Optional. Array with values. If no values are specified, index will be returned.
* @return
*/
RandomUtil.pickWeighted = function(weights, values)
{
//create cumulative weights
var cw = [];
var sum = 0;
for (var i = 0; i < weights.length; i++)
{
sum += weights[i];
cw.push(sum);
}
var idx = ArrayUtil.indexOfClosest(cw, Math.random() * sum, ArrayUtil.HIGHER);
return values ? values[idx] : idx;
}