octo-deno/random/random.js

163 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-04-22 20:16:47 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
2017-04-07 08:10:59 +00:00
var random = {
2017-04-06 16:12:24 +00:00
twister: null,
2017-04-22 19:42:54 +00:00
/**
2017-04-22 20:07:29 +00:00
* Must be called before any other methods can be called to initialize MersenneTwister.
* @param {number|null|undefined} seed Value to initialize MersenneTwister.
*/
2017-04-06 16:12:24 +00:00
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.
2017-04-06 16:12:24 +00:00
if (limit == 0) {
return limit;
}
if (limit == null || limit === undefined) {
limit = 0xffffffff;
}
let x = (0x100000000 / limit) >>> 0,
2017-04-22 20:07:29 +00:00
y = (x * limit) >>> 0, r;
do {
r = this.twister.int32();
2017-04-22 20:07:29 +00:00
} while (y && r >= y);
return (r / x) >>> 0;
2017-04-06 16:12:24 +00:00
},
float: function () {
// Returns a float in [0, 1). Uniform distribution.
return this.twister.real2();
2017-04-06 16:12:24 +00:00
},
range: function (start, limit) {
// Returns an integer in [start, limit]. Uniform distribution.
if (isNaN(start) || isNaN(limit)) {
logger.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
}
return this.number(limit - start + 1) + start;
2017-04-06 16:12:24 +00:00
},
2017-04-22 20:07:29 +00:00
ludOneTo: function (limit) {
2017-04-06 16:12:24 +00:00
// Returns a float in [1, limit]. The logarithm has uniform distribution.
return Math.exp(this.float() * Math.log(limit));
2017-04-06 16:12:24 +00:00
},
2017-04-07 14:59:10 +00:00
item: function (list) {
if (!(list instanceof Array || (list !== undefined && typeof list != "string" && list.hasOwnProperty("length")))) {
logger.traceback();
throw new TypeError("this.item() received a non array type: '" + list + "'");
2017-04-06 16:12:24 +00:00
}
return list[this.number(list.length)];
},
key: function (obj) {
let list = [];
for (let i in obj) {
2017-04-06 16:12:24 +00:00
list.push(i);
}
return this.item(list);
2017-04-06 16:12:24 +00:00
},
bool: function () {
return this.item([true, false]);
2017-04-06 16:12:24 +00:00
},
pick: function (obj) {
if (typeof obj == "function") {
return obj();
}
if (obj instanceof Array) {
return this.pick(this.item(obj));
2017-04-06 16:12:24 +00:00
}
return obj;
},
chance: function (limit) {
if (limit == null || limit === undefined) {
limit = 2;
}
if (isNaN(limit)) {
logger.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)) {
logger.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
}
let total = 0;
for (let i = 0; i < list.length; i++) {
2017-04-06 16:12:24 +00:00
total += list[i][0];
}
let n = this.number(total);
for (let i = 0; i < list.length; i++) {
2017-04-06 16:12:24 +00:00
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
let a = [];
for (let i = 0; i < wa.length; ++i) {
for (let j = 0; j < wa[i].w; ++j) {
2017-04-06 16:12:24 +00:00
a.push(wa[i].v);
}
}
return a;
},
use: function (obj) {
return this.bool() ? obj : "";
2017-04-06 16:12:24 +00:00
},
shuffle: function (arr) {
2017-04-22 21:02:26 +00:00
let i = arr.length;
2017-04-06 16:12:24 +00:00
while (i--) {
let p = this.number(i + 1);
let t = arr[i];
2017-04-06 16:12:24 +00:00
arr[i] = arr[p];
arr[p] = t;
}
},
shuffled: function (arr) {
let newArray = arr.slice();
this.shuffle(newArray);
2017-04-06 16:12:24 +00:00
return newArray;
},
2017-04-07 14:59:43 +00:00
subset: function (list, limit) {
2017-04-06 16:12:24 +00:00
if (!(list instanceof Array)) {
logger.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.number(list.length + 1);
2017-04-06 16:12:24 +00:00
}
let result = [];
for (let i = 0; i < limit; i++) {
2017-04-06 16:12:24 +00:00
result.push(this.pick(list));
}
return result;
2017-04-12 14:40:50 +00:00
},
pop: function (arr) {
// Removes and returns a random item from an array
2017-04-12 15:24:44 +00:00
let i, obj;
2017-04-12 14:40:50 +00:00
i = this.number(arr.length);
obj = arr[i];
arr.splice(i, 1);
2017-04-12 14:40:50 +00:00
return obj;
2017-04-06 16:12:24 +00:00
}
};