octo-deno/lib/random/random.ts

251 lines
5.4 KiB
TypeScript
Raw Normal View History

2017-04-22 22:49:49 +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/. */
2020-02-04 12:49:42 +00:00
import MersenneTwister from "./mersennetwister.ts";
export default class random {
static twister: MersenneTwister;
2018-03-17 22:02:23 +00:00
2017-04-22 22:49:49 +00:00
/**
* Must be called before any other methods can be called to initialize MersenneTwister
2018-09-04 17:34:07 +00:00
* @param {?number} seed - Value to initialize MersenneTwister
2017-04-22 22:49:49 +00:00
*/
2020-02-04 12:49:42 +00:00
static init(seed = null) {
2018-09-04 17:34:07 +00:00
if (seed === null) {
2020-02-04 12:49:42 +00:00
seed = new Date().getTime();
2018-09-04 17:34:07 +00:00
}
2020-02-04 12:49:42 +00:00
random.twister = new MersenneTwister();
random.twister.seed(seed);
}
/**
2018-07-26 23:44:34 +00:00
* Returns an integer in [0, limit) (uniform distribution)
* @param {number} limit
*/
2020-02-04 12:49:42 +00:00
static number(limit = 0xffffffff) {
2018-04-06 22:15:55 +00:00
if (!random.twister) {
2020-02-04 12:49:42 +00:00
throw new Error("random.init must be called first.");
2018-04-06 22:15:55 +00:00
}
2020-02-04 12:49:42 +00:00
let x = (0x100000000 / limit) >>> 0;
let y = (x * limit) >>> 0;
let r;
2017-04-22 22:49:49 +00:00
do {
2020-02-04 12:49:42 +00:00
r = random.twister.int32();
} while (y && r >= y); // eslint-disable-line no-unmodified-loop-condition
return (r / x) >>> 0;
}
/**
2018-07-26 23:44:34 +00:00
* Returns a float in [0, 1) (uniform distribution)
*/
2020-02-04 12:49:42 +00:00
static float() {
2018-04-06 22:15:55 +00:00
if (!random.twister) {
2020-02-04 12:49:42 +00:00
throw new Error("random.init must be called first.");
2018-04-06 22:15:55 +00:00
}
2020-02-04 12:49:42 +00:00
return random.twister.real2();
}
/**
2018-07-26 23:44:34 +00:00
* Returns an integer in [start, limit) (uniform distribution)
* @param {number} start
* @param {number} limit
*/
2020-02-04 12:49:42 +00:00
static range(start, limit) {
2018-04-06 22:15:55 +00:00
if (!random.twister) {
2020-02-04 12:49:42 +00:00
throw new Error("random.init must be called first.");
2018-04-06 22:15:55 +00:00
}
2018-07-26 23:44:34 +00:00
2017-04-22 22:49:49 +00:00
if (isNaN(start) || isNaN(limit)) {
2020-02-04 12:49:42 +00:00
throw new TypeError(
`random.range() received non-number type: (${start}, ${limit})`
);
2017-04-22 22:49:49 +00:00
}
2018-07-26 23:44:34 +00:00
2020-02-04 12:49:42 +00:00
return random.number(limit - start + 1) + start;
}
/**
2018-07-26 23:44:34 +00:00
* Returns a float in [1, limit). The logarithm has uniform distribution.
* @param {number} limit
*/
2020-02-04 12:49:42 +00:00
static ludOneTo(limit) {
return Math.exp(random.float() * Math.log(limit));
}
/**
* Returns a random index from a list
* @param {Array} list
2018-08-13 14:59:05 +00:00
* @returns {*}
*/
2020-02-04 12:49:42 +00:00
static item<T>(list: T[]): T {
if (!Array.isArray(list)) {
2020-02-04 12:49:42 +00:00
throw new TypeError(`random.item() received invalid object: (${list})`);
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
return list[random.number(list.length)];
}
/**
* Returns a random key of a provided object
* @param {Object} obj
*/
2020-02-04 12:49:42 +00:00
static key(obj) {
return random.item(Object.keys(obj));
}
/**
* Return a random Boolean value
*/
2020-02-04 12:49:42 +00:00
static bool() {
return random.item([true, false]);
}
/**
* Recursively iterate over array until non-array item identified
* If item is a function, evaluate it with no args
* @param {*} obj
* @returns {*}
*/
2020-02-04 12:49:42 +00:00
static pick(obj) {
if (typeof obj === "function") {
return obj();
} else if (Array.isArray(obj)) {
2020-02-04 12:49:42 +00:00
return random.pick(random.item(obj));
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
return obj;
}
/**
* Returns a boolean result based on limit
* @param limit
* @returns {boolean}
*/
2020-02-04 12:49:42 +00:00
static chance(limit = 2) {
2017-04-22 22:49:49 +00:00
if (isNaN(limit)) {
2020-02-04 12:49:42 +00:00
throw new TypeError(
`random.chance() received non-number type: (${limit})`
);
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
return random.number(limit) === 1;
}
/**
* Return an item from an array of arrays where the first index in each sub-array denotes the weight
* @param {Array} list - Array of arrays
* @param {Boolean} flat - Indicates whether we should iterate over the arrays recursively
* @returns {*}
*/
2020-02-04 12:49:42 +00:00
static choose(list, flat = false) {
if (!Array.isArray(list)) {
throw new TypeError(`random.choose() received non-array type: (${list})`);
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
const expanded = [];
2018-09-04 17:34:20 +00:00
list.forEach(([weight, value]) => {
while (weight--) {
2020-02-04 12:49:42 +00:00
expanded.push(value);
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
});
if (flat) {
2020-02-04 12:49:42 +00:00
return random.item(expanded);
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
return random.pick(expanded);
}
/**
2018-09-04 17:34:20 +00:00
* Return a flattened list of weighted values
* [{w: 1, v: 'foo'}, {w: 1, v: 'bar'}]
* @param {Array} list
* @param {Array}
*/
2020-02-04 12:49:42 +00:00
static weighted(list) {
const expanded = [];
list.forEach(item => {
2018-09-04 17:34:20 +00:00
while (item.w--) {
2020-02-04 12:49:42 +00:00
expanded.push(item.v);
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
});
2020-02-04 12:49:42 +00:00
return expanded;
}
2020-02-04 12:49:42 +00:00
static use(obj) {
return random.bool() ? obj : "";
}
/**
* Returns arr shuffled
* @param arr
*/
2020-02-04 12:49:42 +00:00
static shuffle(arr) {
let i = arr.length;
2017-04-22 22:49:49 +00:00
while (i--) {
2020-02-04 12:49:42 +00:00
let p = random.number(i + 1);
let t = arr[i];
arr[i] = arr[p];
arr[p] = t;
2017-04-22 22:49:49 +00:00
}
}
/**
* Returns a shuffled copy of arr
* @param arr
* @returns {*}
*/
2020-02-04 12:49:42 +00:00
static shuffled(arr) {
let newArray = arr.slice();
random.shuffle(newArray);
return newArray;
}
/**
* Select an array containing a subset of 'list'
* @param list
* @param limit
* @returns {Array}
*/
2020-02-04 12:49:42 +00:00
static subset(list, limit?) {
if (!Array.isArray(list)) {
throw new TypeError(`random.subset() received non-array type: (${list})`);
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
if (typeof limit !== "number") {
limit = random.number(list.length + 1);
2017-04-22 22:49:49 +00:00
}
// Deepclone list
2020-02-04 12:49:42 +00:00
const temp = JSON.parse(JSON.stringify(list));
const result = [];
while (limit--) {
2020-02-04 12:49:42 +00:00
result.push(random.pop(temp));
2017-04-22 22:49:49 +00:00
}
2020-02-04 12:49:42 +00:00
return result;
}
/**
* Removes and returns a random item from an array.
* @param {*} arr
*/
2020-02-04 12:49:42 +00:00
static pop(arr) {
let i, obj;
2017-04-22 22:49:49 +00:00
2020-02-04 12:49:42 +00:00
i = random.number(arr.length);
obj = arr[i];
arr.splice(i, 1);
2017-04-22 22:49:49 +00:00
2020-02-04 12:49:42 +00:00
return obj;
}
2020-02-04 12:49:42 +00:00
static hex(len) {
return random.number(Math.pow(2, len * 4)).toString(16);
}
}