octo-deno/random/random.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-04-07 08:10:59 +00:00
var random = {
2017-04-06 16:12:24 +00:00
twister: null,
init: function (seed) {
if (seed == null || seed === undefined) {
seed = new Date().getTime();
}
this.twister = new MersenneTwister();
this.twister.seed(seed);
},
number: function (limit) {
// Returns an integer in [0, limit]. Uniform distribution.
if (limit == 0) {
return limit;
}
if (limit == null || limit === undefined) {
limit = 0xffffffff;
}
return (this.twister.int32() >>> 0) % limit;
},
float: function () {
// Returns a float in [0, 1]. Uniform distribution.
return this.twister.real2() >>> 0;
},
range: function (start, limit) {
// Returns an integer in [start, limit]. Uniform distribution.
if (isNaN(start) || isNaN(limit)) {
Utils.traceback();
2017-04-07 08:10:59 +00:00
throw new TypeError("random.range() received a non number type: '" + start + "', '" + limit + "')");
2017-04-06 16:12:24 +00:00
}
2017-04-07 08:10:59 +00:00
return random.number(limit - start + 1) + start;
2017-04-06 16:12:24 +00:00
},
ludOneTo: function(limit) {
// Returns a float in [1, limit]. The logarithm has uniform distribution.
2017-04-07 08:10:59 +00:00
return Math.exp(random.float() * Math.log(limit));
2017-04-06 16:12:24 +00:00
},
index: function (list) {
if (!(list instanceof Array || (typeof list != "string" && "length" in list))) {
Utils.traceback();
2017-04-07 08:10:59 +00:00
throw new TypeError("random.index() received a non array type: '" + list + "'");
2017-04-06 16:12:24 +00:00
}
return list[this.number(list.length)];
},
key: function (obj) {
var list = [];
for (var i in obj) {
list.push(i);
}
return this.index(list);
},
bool: function () {
return this.index([true, false]);
},
pick: function (obj) {
if (typeof obj == "function") {
return obj();
}
if (obj instanceof Array) {
return this.pick(this.index(obj));
}
return obj;
},
chance: function (limit) {
if (limit == null || limit === undefined) {
limit = 2;
}
if (isNaN(limit)) {
Utils.traceback();
2017-04-07 08:10:59 +00:00
throw new TypeError("random.chance() received a non number type: '" + limit + "'");
2017-04-06 16:12:24 +00:00
}
return this.number(limit) == 1;
},
choose: function (list, flat) {
if (!(list instanceof Array)) {
Utils.traceback();
2017-04-07 08:10:59 +00:00
throw new TypeError("random.choose() received a non-array type: '" + list + "'");
2017-04-06 16:12:24 +00:00
}
var total = 0;
for (var i = 0; i < list.length; i++) {
total += list[i][0];
}
var n = this.number(total);
for (var i = 0; i < list.length; i++) {
if (n < list[i][0]) {
if (flat == true) {
return list[i][1];
} else {
return this.pick([list[i][1]]);
}
}
n = n - list[i][0];
}
if (flat == true) {
return list[0][1];
}
return this.pick([list[0][1]]);
},
weighted: function (wa) {
2017-04-07 08:10:59 +00:00
// More memory-hungry but hopefully faster than random.choose$flat
2017-04-06 16:12:24 +00:00
var a = [];
for (var i = 0; i < wa.length; ++i) {
for (var j = 0; j < wa[i].w; ++j) {
a.push(wa[i].v);
}
}
return a;
},
use: function (obj) {
2017-04-07 08:10:59 +00:00
return random.bool() ? obj : "";
2017-04-06 16:12:24 +00:00
},
shuffle: function (arr) {
var len = arr.length;
var i = len;
while (i--) {
2017-04-07 08:10:59 +00:00
var p = random.number(i + 1);
2017-04-06 16:12:24 +00:00
var t = arr[i];
arr[i] = arr[p];
arr[p] = t;
}
},
shuffled: function (arr) {
var newArray = arr.slice();
2017-04-07 08:10:59 +00:00
random.shuffle(newArray);
2017-04-06 16:12:24 +00:00
return newArray;
},
some: function (list, limit) {
if (!(list instanceof Array)) {
Utils.traceback();
2017-04-07 08:10:59 +00:00
throw new TypeError("random.some() received a non-array type: '" + list + "'");
2017-04-06 16:12:24 +00:00
}
if (typeof limit == 'number') {
limit = this.range(0, list.length);
}
var result = [];
for (var i = 0; i < limit; i++) {
result.push(this.pick(list));
}
return result;
}
};