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/. */
|
|
|
|
|
2018-03-17 22:02:23 +00:00
|
|
|
const MersenneTwister = require('./mersennetwister')
|
2018-03-19 03:23:05 +00:00
|
|
|
const {logger} = require('../logging')
|
2018-03-17 22:02:23 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
class random {
|
2017-04-22 22:49:49 +00:00
|
|
|
/**
|
2018-07-26 23:27:25 +00:00
|
|
|
* Must be called before any other methods can be called to initialize MersenneTwister
|
2018-08-27 22:57:27 +00:00
|
|
|
* @param {number} seed - Value to initialize MersenneTwister
|
2017-04-22 22:49:49 +00:00
|
|
|
*/
|
2018-08-27 22:57:27 +00:00
|
|
|
static init (seed = new Date().getTime()) {
|
2018-04-06 22:15:55 +00:00
|
|
|
random.twister = new MersenneTwister()
|
2018-08-27 22:57:39 +00:00
|
|
|
random.twister.seed(seed)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-26 23:44:34 +00:00
|
|
|
* Returns an integer in [0, limit) (uniform distribution)
|
2018-07-26 23:27:25 +00:00
|
|
|
* @param {number} limit
|
2018-03-19 03:23:05 +00:00
|
|
|
*/
|
2018-08-27 22:57:39 +00:00
|
|
|
static number (limit = 0xffffffff) {
|
2018-04-06 22:15:55 +00:00
|
|
|
if (!random.twister) {
|
|
|
|
throw new Error('random.init must be called first.')
|
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
let x = (0x100000000 / limit) >>> 0
|
|
|
|
let y = (x * limit) >>> 0
|
|
|
|
let r
|
2017-04-22 22:49:49 +00:00
|
|
|
do {
|
2018-04-06 22:15:55 +00:00
|
|
|
r = random.twister.int32()
|
2017-04-25 22:21:31 +00:00
|
|
|
} while (y && r >= y) // eslint-disable-line no-unmodified-loop-condition
|
|
|
|
return (r / x) >>> 0
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-26 23:44:34 +00:00
|
|
|
* Returns a float in [0, 1) (uniform distribution)
|
2018-03-19 03:23:05 +00:00
|
|
|
*/
|
|
|
|
static float () {
|
2018-04-06 22:15:55 +00:00
|
|
|
if (!random.twister) {
|
|
|
|
throw new Error('random.init must be called first.')
|
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.twister.real2()
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-26 23:44:34 +00:00
|
|
|
* Returns an integer in [start, limit) (uniform distribution)
|
2018-07-26 23:27:25 +00:00
|
|
|
* @param {number} start
|
|
|
|
* @param {number} limit
|
2018-03-19 03:23:05 +00:00
|
|
|
*/
|
|
|
|
static range (start, limit) {
|
2018-04-06 22:15:55 +00:00
|
|
|
if (!random.twister) {
|
|
|
|
throw new Error('random.init must be called first.')
|
|
|
|
}
|
2018-07-26 23:44:34 +00:00
|
|
|
|
2017-04-22 22:49:49 +00:00
|
|
|
if (isNaN(start) || isNaN(limit)) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2018-03-19 03:23:05 +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
|
|
|
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.number(limit - start + 1) + start
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-26 23:44:34 +00:00
|
|
|
* Returns a float in [1, limit). The logarithm has uniform distribution.
|
2018-07-26 23:27:25 +00:00
|
|
|
* @param {number} limit
|
2018-03-19 03:23:05 +00:00
|
|
|
*/
|
|
|
|
static ludOneTo (limit) {
|
2018-04-06 22:15:55 +00:00
|
|
|
return Math.exp(random.float() * Math.log(limit))
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:25 +00:00
|
|
|
/**
|
|
|
|
* Returns a random index from a list
|
|
|
|
* @param {Array} list
|
2018-08-13 14:59:05 +00:00
|
|
|
* @returns {*}
|
2018-07-26 23:27:25 +00:00
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static item (list) {
|
2018-07-26 23:27:25 +00:00
|
|
|
if (!Array.isArray(list)) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2018-03-19 03:23:05 +00:00
|
|
|
throw new TypeError(`random.item() received invalid object: (${list})`)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2018-04-06 22:15:55 +00:00
|
|
|
return list[random.number(list.length)]
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-26 23:27:25 +00:00
|
|
|
* Returns a random key of a provided object
|
|
|
|
* @param {Object} obj
|
2018-03-19 03:23:05 +00:00
|
|
|
*/
|
|
|
|
static key (obj) {
|
2018-07-26 23:27:25 +00:00
|
|
|
return random.item(Object.keys(obj))
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-26 23:27:25 +00:00
|
|
|
* Return a random Boolean value
|
2018-03-19 03:23:05 +00:00
|
|
|
*/
|
|
|
|
static bool () {
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.item([true, false])
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:25 +00:00
|
|
|
/**
|
|
|
|
* Recursively iterate over array until non-array item identified
|
|
|
|
* If item is a function, evaluate it with no args
|
|
|
|
* @param {*} obj
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static pick (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
if (typeof obj === 'function') {
|
|
|
|
return obj()
|
2018-07-26 23:27:25 +00:00
|
|
|
} else if (Array.isArray(obj)) {
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.pick(random.item(obj))
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
return obj
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:25 +00:00
|
|
|
/**
|
|
|
|
* Returns a boolean result based on limit
|
|
|
|
* @param limit
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
static chance (limit = 2) {
|
2017-04-22 22:49:49 +00:00
|
|
|
if (isNaN(limit)) {
|
2017-04-25 22:21:31 +00:00
|
|
|
logger.traceback()
|
2018-03-19 03:23:05 +00:00
|
|
|
throw new TypeError(`random.chance() received non-number type: (${limit})`)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.number(limit) === 1
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:25 +00:00
|
|
|
/**
|
|
|
|
* 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 {*}
|
|
|
|
*/
|
|
|
|
static choose (list, flat = false) {
|
2017-06-08 13:31:41 +00:00
|
|
|
if (!(Array.isArray(list))) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2018-03-19 03:23:05 +00:00
|
|
|
throw new TypeError(`random.choose() received non-array type: (${list})`)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
let total = 0
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let i = 0; i < list.length; i++) {
|
2017-04-25 22:21:31 +00:00
|
|
|
total += list[i][0]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2018-04-06 22:15:55 +00:00
|
|
|
let n = random.number(total)
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
if (n < list[i][0]) {
|
2017-04-25 15:22:15 +00:00
|
|
|
if (flat === true) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return list[i][1]
|
2017-04-22 22:49:49 +00:00
|
|
|
} else {
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.pick([list[i][1]])
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
n = n - list[i][0]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
|
|
|
if (flat) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return list[0][1]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.pick([list[0][1]])
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* More memory-hungry but hopefully faster than random.choose$flat.
|
|
|
|
* @param {*} wa
|
|
|
|
*/
|
|
|
|
static weighted (wa) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let a = []
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let i = 0; i < wa.length; ++i) {
|
|
|
|
for (let j = 0; j < wa[i].w; ++j) {
|
2017-04-25 22:21:31 +00:00
|
|
|
a.push(wa[i].v)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-26 23:27:25 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
return a
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static use (obj) {
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.bool() ? obj : ''
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:25 +00:00
|
|
|
/**
|
|
|
|
* Returns arr shuffled
|
|
|
|
* @param arr
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static shuffle (arr) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let i = arr.length
|
2017-04-22 22:49:49 +00:00
|
|
|
while (i--) {
|
2018-04-06 22:15:55 +00:00
|
|
|
let p = random.number(i + 1)
|
2017-04-25 22:21:31 +00:00
|
|
|
let t = arr[i]
|
|
|
|
arr[i] = arr[p]
|
|
|
|
arr[p] = t
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:27:25 +00:00
|
|
|
/**
|
|
|
|
* Returns a shuffled copy of arr
|
|
|
|
* @param arr
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static shuffled (arr) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let newArray = arr.slice()
|
2018-04-06 22:15:55 +00:00
|
|
|
random.shuffle(newArray)
|
2017-04-25 22:21:31 +00:00
|
|
|
return newArray
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 23:26:27 +00:00
|
|
|
/**
|
|
|
|
* Select an array containing a subset of 'list'
|
|
|
|
* @param list
|
|
|
|
* @param limit
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static subset (list, limit) {
|
2017-06-08 13:31:41 +00:00
|
|
|
if (!(Array.isArray(list))) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2018-03-19 03:23:05 +00:00
|
|
|
throw new TypeError(`random.subset() received non-array type: (${list})`)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:26:27 +00:00
|
|
|
|
2017-04-22 22:49:49 +00:00
|
|
|
if (typeof limit !== 'number') {
|
2018-04-06 22:15:55 +00:00
|
|
|
limit = random.number(list.length + 1)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:26:27 +00:00
|
|
|
|
|
|
|
// Deepclone list
|
|
|
|
const temp = JSON.parse(JSON.stringify(list))
|
|
|
|
const result = []
|
|
|
|
while (limit--) {
|
|
|
|
result.push(random.pop(temp))
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-07-26 23:26:27 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
return result
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes and returns a random item from an array.
|
|
|
|
* @param {*} arr
|
|
|
|
*/
|
|
|
|
static pop (arr) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let i, obj
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2018-04-06 22:15:55 +00:00
|
|
|
i = random.number(arr.length)
|
2017-04-25 22:21:31 +00:00
|
|
|
obj = arr[i]
|
|
|
|
arr.splice(i, 1)
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
return obj
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static hex (len) {
|
2018-04-06 22:15:55 +00:00
|
|
|
return random.number(Math.pow(2, len * 4)).toString(16)
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-17 22:02:23 +00:00
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
module.exports = random
|