From 075fe8930927b47ef0738be3327f86ac8d27205c Mon Sep 17 00:00:00 2001 From: pyoor Date: Thu, 26 Jul 2018 19:26:27 -0400 Subject: [PATCH 1/4] Refactor random.subset to pop items from cloned array --- lib/random/random.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index 63a1f61..eb4b901 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -180,18 +180,29 @@ class random { return newArray } + /** + * Select an array containing a subset of 'list' + * @param list + * @param limit + * @returns {Array} + */ static subset (list, limit) { if (!(Array.isArray(list))) { logger.traceback() throw new TypeError(`random.subset() received non-array type: (${list})`) } + if (typeof limit !== 'number') { limit = random.number(list.length + 1) } - let result = [] - for (let i = 0; i < limit; i++) { - result.push(random.pick(list)) + + // Deepclone list + const temp = JSON.parse(JSON.stringify(list)) + const result = [] + while (limit--) { + result.push(random.pop(temp)) } + return result } From 625adebc67d96ef9c07add00cd75dd09d8679b39 Mon Sep 17 00:00:00 2001 From: pyoor Date: Thu, 26 Jul 2018 19:26:57 -0400 Subject: [PATCH 2/4] Fix uses of random.subset --- lib/make/network.js | 12 +++++++----- lib/make/webgl.js | 11 ++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/make/network.js b/lib/make/network.js index 505d6c8..ce4e233 100644 --- a/lib/make/network.js +++ b/lib/make/network.js @@ -141,11 +141,13 @@ class network extends make { } static dtmf () { - return random.subset([ - '*', '#', - 'A', 'B', 'C', 'D', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' - ], make.number.range()).join('') + let count = make.number.range() + const values = [] + while (count--) { + values.push(random.item(['*', '#', 'A', 'B', 'C', 'D', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])) + } + + return values.join('') } static goodHostnames () { diff --git a/lib/make/webgl.js b/lib/make/webgl.js index 14e2400..6a4cce0 100644 --- a/lib/make/webgl.js +++ b/lib/make/webgl.js @@ -72,7 +72,7 @@ class webgl extends make { static parseUniforms (shader, group = 1) { /* Todo: Parse their individual data types into categories. */ - return webgl.match(shader, /uniform .+? (\w+)(?=[\[;])/gm, group) /* eslint-disable-line no-useless-escape */ + return webgl.match(shader, /uniform .+? (\w+)(?=[\[;])/gm, group) // eslint-disable-line no-useless-escape } static parseAttributes (shader, group = 1) { @@ -90,7 +90,7 @@ class webgl extends make { static parseFragDatav3 (shader, group = 1) { // #version 300 - return webgl.match(shader, /out .+? (\w+)(?=[\[;])/gm, group) /* eslint-disable-line no-useless-escape */ + return webgl.match(shader, /out .+? (\w+)(?=[\[;])/gm, group) // eslint-disable-line no-useless-escape } static parseFrag (shader, group = 1) { @@ -102,7 +102,12 @@ class webgl extends make { } static randomBitmask () { - return parseInt((random.subset([1, 0], 8).join(''))) + const values = [] + for (let i = 0; i < 8; i++) { + values.push(random.item([1, 0])) + } + + return parseInt(values.join('')) } static randomBufferTarget (isWebGL2) { From 2786f886fceca50af7a99d8b9c1e732a57e84249 Mon Sep 17 00:00:00 2001 From: pyoor Date: Thu, 26 Jul 2018 19:27:25 -0400 Subject: [PATCH 3/4] Added JSDoc throughout and minor refactoring --- lib/random/random.js | 92 +++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index eb4b901..38e1034 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -7,28 +7,31 @@ const {logger} = require('../logging') class random { /** - * Must be called before any other methods can be called to initialize MersenneTwister. - * @param {number|null|undefined} seed Value to initialize MersenneTwister. + * 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) { if (seed === null || seed === undefined) { random.seed = new Date().getTime() } + random.twister = new MersenneTwister() random.twister.seed(random.seed) } /** - * Returns an integer in [0, limit). Uniform distribution. - * @param limit + * Returns an integer in [0, limit] (uniform distribution) + * @param {number} limit */ static number (limit) { if (!random.twister) { throw new Error('random.init must be called first.') } + if (limit === null || limit === undefined) { limit = 0xffffffff } + let x = (0x100000000 / limit) >>> 0 let y = (x * limit) >>> 0 let r @@ -39,19 +42,20 @@ class random { } /** - * Returns a float in [0, 1). Uniform distribution. + * Returns a float in [0, 1] (uniform distribution) */ static float () { if (!random.twister) { throw new Error('random.init must be called first.') } + return random.twister.real2() } /** - * Returns an integer in [start, limit]. Uniform distribution. - * @param start - * @param limit + * Returns an integer in [start, limit] (uniform distribution) + * @param {number} start + * @param {number} limit */ static range (start, limit) { if (!random.twister) { @@ -66,69 +70,93 @@ class random { /** * Returns a float in [1, limit]. The logarithm has uniform distribution. - * @param {*} limit + * @param {number} limit */ static ludOneTo (limit) { return Math.exp(random.float() * Math.log(limit)) } + /** + * Returns a random index from a list + * @param {Array} list + * @returns list[n] + */ static item (list) { - if (list === undefined || typeof list === 'string' || list.length === undefined) { + if (!Array.isArray(list)) { logger.traceback() throw new TypeError(`random.item() received invalid object: (${list})`) } + return list[random.number(list.length)] } /** - * Returns a random key of a provided object. - * @param {*} obj + * Returns a random key of a provided object + * @param {Object} obj */ static key (obj) { - let list = [] - for (let i in obj) { - list.push(i) - } - return random.item(list) + return random.item(Object.keys(obj)) } /** - * Return a random Boolean value. + * Return a random Boolean value */ 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 {*} + */ static pick (obj) { + if (!(Array.isArray(obj) || typeof obj === 'function')) { + logger.traceback() + throw new TypeError(`random.pick() received invalid object: (${obj})`) + } + if (typeof obj === 'function') { return obj() - } - if (Array.isArray(obj)) { + } else if (Array.isArray(obj)) { return random.pick(random.item(obj)) } + return obj } - static chance (limit) { - if (limit === null || limit === undefined) { - limit = 2 - } + /** + * Returns a boolean result based on limit + * @param limit + * @returns {boolean} + */ + static chance (limit = 2) { if (isNaN(limit)) { logger.traceback() throw new TypeError(`random.chance() received non-number type: (${limit})`) } + return random.number(limit) === 1 } - static choose (list, flat) { + /** + * 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) { if (!(Array.isArray(list))) { logger.traceback() throw new TypeError(`random.choose() received non-array type: (${list})`) } + let total = 0 for (let i = 0; i < list.length; i++) { total += list[i][0] } + let n = random.number(total) for (let i = 0; i < list.length; i++) { if (n < list[i][0]) { @@ -140,9 +168,11 @@ class random { } n = n - list[i][0] } - if (flat === true) { + + if (flat) { return list[0][1] } + return random.pick([list[0][1]]) } @@ -157,6 +187,7 @@ class random { a.push(wa[i].v) } } + return a } @@ -164,6 +195,10 @@ class random { return random.bool() ? obj : '' } + /** + * Returns arr shuffled + * @param arr + */ static shuffle (arr) { let i = arr.length while (i--) { @@ -174,6 +209,11 @@ class random { } } + /** + * Returns a shuffled copy of arr + * @param arr + * @returns {*} + */ static shuffled (arr) { let newArray = arr.slice() random.shuffle(newArray) From e87962069a22a6b2ea6e4ae758297d7677439f7c Mon Sep 17 00:00:00 2001 From: pyoor Date: Thu, 26 Jul 2018 19:44:34 -0400 Subject: [PATCH 4/4] Minor fixes --- lib/random/random.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index 38e1034..403a59a 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -20,7 +20,7 @@ class random { } /** - * Returns an integer in [0, limit] (uniform distribution) + * Returns an integer in [0, limit) (uniform distribution) * @param {number} limit */ static number (limit) { @@ -42,7 +42,7 @@ class random { } /** - * Returns a float in [0, 1] (uniform distribution) + * Returns a float in [0, 1) (uniform distribution) */ static float () { if (!random.twister) { @@ -53,7 +53,7 @@ class random { } /** - * Returns an integer in [start, limit] (uniform distribution) + * Returns an integer in [start, limit) (uniform distribution) * @param {number} start * @param {number} limit */ @@ -61,15 +61,17 @@ class random { if (!random.twister) { throw new Error('random.init must be called first.') } + if (isNaN(start) || isNaN(limit)) { logger.traceback() throw new TypeError(`random.range() received non-number type: (${start}, ${limit})`) } + return random.number(limit - start + 1) + start } /** - * Returns a float in [1, limit]. The logarithm has uniform distribution. + * Returns a float in [1, limit). The logarithm has uniform distribution. * @param {number} limit */ static ludOneTo (limit) {