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/. */
|
|
|
|
|
|
|
|
var random = {
|
|
|
|
twister: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Must be called before any other methods can be called to initialize MersenneTwister.
|
|
|
|
* @param {number|null|undefined} seed Value to initialize MersenneTwister.
|
|
|
|
*/
|
|
|
|
init: function (seed) {
|
2017-04-25 15:22:15 +00:00
|
|
|
if (seed === null || seed === undefined) {
|
2017-04-25 22:21:31 +00:00
|
|
|
seed = new Date().getTime()
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
this.twister = new MersenneTwister()
|
|
|
|
this.twister.seed(seed)
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
number: function (limit) {
|
|
|
|
// Returns an integer in [0, limit). Uniform distribution.
|
2017-04-25 15:22:15 +00:00
|
|
|
if (limit === 0) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return limit
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 15:22:15 +00:00
|
|
|
if (limit === null || limit === undefined) {
|
2017-04-25 22:21:31 +00:00
|
|
|
limit = 0xffffffff
|
2017-04-22 22:49:49 +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 {
|
2017-04-25 22:21:31 +00:00
|
|
|
r = this.twister.int32()
|
|
|
|
} while (y && r >= y) // eslint-disable-line no-unmodified-loop-condition
|
|
|
|
return (r / x) >>> 0
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
float: function () {
|
|
|
|
// Returns a float in [0, 1). Uniform distribution.
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.twister.real2()
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
range: function (start, limit) {
|
|
|
|
// Returns an integer in [start, limit]. Uniform distribution.
|
|
|
|
if (isNaN(start) || isNaN(limit)) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2017-04-25 22:21:31 +00:00
|
|
|
throw new TypeError('random.range() received a non number type: \'' + start + '\', \'' + limit + '\')')
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.number(limit - start + 1) + start
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
ludOneTo: function (limit) {
|
|
|
|
// Returns a float in [1, limit]. The logarithm has uniform distribution.
|
2017-04-25 22:21:31 +00:00
|
|
|
return Math.exp(this.float() * Math.log(limit))
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
item: function (list) {
|
2017-04-25 22:21:31 +00:00
|
|
|
if (!(list instanceof Array || (list !== undefined && typeof list !== 'string' && list.hasOwnProperty('length')))) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2017-04-25 22:21:31 +00:00
|
|
|
throw new TypeError('this.item() received a non array type: \'' + list + '\'')
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return list[this.number(list.length)]
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
key: function (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let list = []
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let i in obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
list.push(i)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.item(list)
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
bool: function () {
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.item([true, false])
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
pick: function (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
if (typeof obj === 'function') {
|
|
|
|
return obj()
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
if (obj instanceof Array) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.pick(this.item(obj))
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return obj
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
chance: function (limit) {
|
2017-04-25 15:22:15 +00:00
|
|
|
if (limit === null || limit === undefined) {
|
2017-04-25 22:21:31 +00:00
|
|
|
limit = 2
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
if (isNaN(limit)) {
|
2017-04-25 22:21:31 +00:00
|
|
|
logger.traceback()
|
|
|
|
throw new TypeError('random.chance() received a non number type: \'' + limit + '\'')
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.number(limit) === 1
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
choose: function (list, flat) {
|
|
|
|
if (!(list instanceof Array)) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2017-04-25 22:21:31 +00:00
|
|
|
throw new TypeError('random.choose() received a non-array type: \'' + list + '\'')
|
2017-04-22 22:49:49 +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
|
|
|
}
|
2017-04-25 22:21:31 +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) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return list[i][1]
|
2017-04-22 22:49:49 +00:00
|
|
|
} else {
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.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
|
|
|
}
|
2017-04-25 15:22:15 +00:00
|
|
|
if (flat === true) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return list[0][1]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.pick([list[0][1]])
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
weighted: function (wa) {
|
|
|
|
// More memory-hungry but hopefully faster than random.choose$flat
|
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
|
|
|
}
|
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return a
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
use: function (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.bool() ? obj : ''
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
shuffle: function (arr) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let i = arr.length
|
2017-04-22 22:49:49 +00:00
|
|
|
while (i--) {
|
2017-04-25 22:21:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
shuffled: function (arr) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let newArray = arr.slice()
|
|
|
|
this.shuffle(newArray)
|
|
|
|
return newArray
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
subset: function (list, limit) {
|
|
|
|
if (!(list instanceof Array)) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2017-04-25 22:21:31 +00:00
|
|
|
throw new TypeError('random.some() received a non-array type: \'' + list + '\'')
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
if (typeof limit !== 'number') {
|
2017-04-25 22:21:31 +00:00
|
|
|
limit = this.number(list.length + 1)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
let result = []
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let i = 0; i < limit; i++) {
|
2017-04-25 22:21:31 +00:00
|
|
|
result.push(this.pick(list))
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return result
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
pop: function (arr) {
|
|
|
|
// Removes and returns a random item from an array
|
2017-04-25 22:21:31 +00:00
|
|
|
let i, obj
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
i = this.number(arr.length)
|
|
|
|
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
|
2017-04-25 17:04:58 +00:00
|
|
|
},
|
2017-04-25 22:21:31 +00:00
|
|
|
hex: function (len) {
|
|
|
|
return Math.floor(Math.random() * ('0x1' + Array(len + 1).join('0'))).toString(16)
|
|
|
|
}
|
|
|
|
}
|