octo-deno/lib/random/random.js

209 lines
4.7 KiB
JavaScript
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/. */
2018-03-17 22:02:23 +00:00
const MersenneTwister = require('./mersennetwister')
const {logger} = require('../logging')
2018-03-17 22:02:23 +00:00
class random {
2017-04-22 22:49:49 +00:00
/**
* Must be called before any other methods can be called to initialize MersenneTwister.
* @param {number|null|undefined} seed Value to initialize MersenneTwister.
*/
static init (seed) {
2017-04-25 15:22:15 +00:00
if (seed === null || seed === undefined) {
2017-06-08 02:12:49 +00:00
this.seed = new Date().getTime()
2017-04-22 22:49:49 +00:00
}
this.twister = new MersenneTwister()
2017-06-08 02:12:49 +00:00
this.twister.seed(this.seed)
}
/**
* Returns an integer in [0, limit). Uniform distribution.
* @param limit
*/
static number (limit) {
2017-04-25 15:22:15 +00:00
if (limit === null || limit === undefined) {
limit = 0xffffffff
2017-04-22 22:49:49 +00:00
}
let x = (0x100000000 / limit) >>> 0
let y = (x * limit) >>> 0
let r
2017-04-22 22:49:49 +00:00
do {
r = this.twister.int32()
} while (y && r >= y) // eslint-disable-line no-unmodified-loop-condition
return (r / x) >>> 0
}
/**
* Returns a float in [0, 1). Uniform distribution.
*/
static float () {
return this.twister.real2()
}
/**
* Returns an integer in [start, limit]. Uniform distribution.
* @param start
* @param limit
*/
static range (start, limit) {
2017-04-22 22:49:49 +00:00
if (isNaN(start) || isNaN(limit)) {
2017-04-25 22:28:31 +00:00
logger.traceback()
throw new TypeError(`random.range() received non-number type: (${start}, ${limit})`)
2017-04-22 22:49:49 +00:00
}
return this.number(limit - start + 1) + start
}
/**
* Returns a float in [1, limit]. The logarithm has uniform distribution.
* @param {*} limit
*/
static ludOneTo (limit) {
return Math.exp(this.float() * Math.log(limit))
}
static item (list) {
if (list === undefined || typeof list === 'string' || list.length === undefined) {
2017-04-25 22:28:31 +00:00
logger.traceback()
throw new TypeError(`random.item() received invalid object: (${list})`)
2017-04-22 22:49:49 +00:00
}
return list[this.number(list.length)]
}
/**
* Returns a random key of a provided object.
* @param {*} obj
*/
static key (obj) {
let list = []
2017-04-22 22:49:49 +00:00
for (let i in obj) {
list.push(i)
2017-04-22 22:49:49 +00:00
}
return this.item(list)
}
/**
* Return a random Boolean value.
*/
static bool () {
return this.item([true, false])
}
static pick (obj) {
if (typeof obj === 'function') {
return obj()
2017-04-22 22:49:49 +00:00
}
if (Array.isArray(obj)) {
return this.pick(this.item(obj))
2017-04-22 22:49:49 +00:00
}
return obj
}
static chance (limit) {
2017-04-25 15:22:15 +00:00
if (limit === null || limit === undefined) {
limit = 2
2017-04-22 22:49:49 +00:00
}
if (isNaN(limit)) {
logger.traceback()
throw new TypeError(`random.chance() received non-number type: (${limit})`)
2017-04-22 22:49:49 +00:00
}
return this.number(limit) === 1
}
static choose (list, flat) {
if (!(Array.isArray(list))) {
2017-04-25 22:28:31 +00:00
logger.traceback()
throw new TypeError(`random.choose() received non-array type: (${list})`)
2017-04-22 22:49:49 +00:00
}
let total = 0
2017-04-22 22:49:49 +00:00
for (let i = 0; i < list.length; i++) {
total += list[i][0]
2017-04-22 22:49:49 +00:00
}
let n = this.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) {
return list[i][1]
2017-04-22 22:49:49 +00:00
} else {
return this.pick([list[i][1]])
2017-04-22 22:49:49 +00:00
}
}
n = n - list[i][0]
2017-04-22 22:49:49 +00:00
}
2017-04-25 15:22:15 +00:00
if (flat === true) {
return list[0][1]
2017-04-22 22:49:49 +00:00
}
return this.pick([list[0][1]])
}
/**
* More memory-hungry but hopefully faster than random.choose$flat.
* @param {*} wa
*/
static weighted (wa) {
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) {
a.push(wa[i].v)
2017-04-22 22:49:49 +00:00
}
}
return a
}
static use (obj) {
return this.bool() ? obj : ''
}
static shuffle (arr) {
let i = arr.length
2017-04-22 22:49:49 +00:00
while (i--) {
let p = this.number(i + 1)
let t = arr[i]
arr[i] = arr[p]
arr[p] = t
2017-04-22 22:49:49 +00:00
}
}
static shuffled (arr) {
let newArray = arr.slice()
this.shuffle(newArray)
return newArray
}
static subset (list, limit) {
if (!(Array.isArray(list))) {
2017-04-25 22:28:31 +00:00
logger.traceback()
throw new TypeError(`random.subset() received non-array type: (${list})`)
2017-04-22 22:49:49 +00:00
}
if (typeof limit !== 'number') {
limit = this.number(list.length + 1)
2017-04-22 22:49:49 +00:00
}
let result = []
2017-04-22 22:49:49 +00:00
for (let i = 0; i < limit; i++) {
result.push(this.pick(list))
2017-04-22 22:49:49 +00:00
}
return result
}
/**
* Removes and returns a random item from an array.
* @param {*} arr
*/
static pop (arr) {
let i, obj
2017-04-22 22:49:49 +00:00
i = this.number(arr.length)
obj = arr[i]
arr.splice(i, 1)
2017-04-22 22:49:49 +00:00
return obj
}
static hex (len) {
2017-06-07 14:27:51 +00:00
return this.number(Math.pow(2, len * 4)).toString(16)
}
}
2018-03-17 22:02:23 +00:00
module.exports = random