From cdf4dc1609b144a23f059406c084c834f73c1b91 Mon Sep 17 00:00:00 2001 From: "Joakim L. Christiansen" <5984627+JoakimCh@users.noreply.github.com> Date: Wed, 5 May 2021 18:47:03 +0200 Subject: [PATCH] Delete private directory --- private/mulberry32.js | 53 --------------------------- private/pluggablePrng.js | 79 ---------------------------------------- private/xmur3.js | 37 ------------------- 3 files changed, 169 deletions(-) delete mode 100644 private/mulberry32.js delete mode 100644 private/pluggablePrng.js delete mode 100644 private/xmur3.js diff --git a/private/mulberry32.js b/private/mulberry32.js deleted file mode 100644 index 23f720f..0000000 --- a/private/mulberry32.js +++ /dev/null @@ -1,53 +0,0 @@ -/* -Mulberry32 was developed by Tommy Ettinger and is in the "public domain": -https://gist.github.com/tommyettinger/46a874533244883189143505d203312c -*/ - -/** - * This is the Mulberry32 PRNG algorithm encased in a class compatible with `pluggable-prng` as the `randomGenerator`. It's compatible with any `seedGenerator` returning unsigned 32-bit integers, e.g. Xmur3. - */ -export class Mulberry32 { - /* - function mulberry32(a) { - return function() { - a |= 0; a = a + 0x6D2B79F5 | 0 - var t = Math.imul(a ^ a >>> 15, 1 | a) - t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t - return ((t ^ t >>> 14) >>> 0) / 4294967296 - } - } - */ - constructor(integerSeed) { - this.state = integerSeed >>> 0 - } - /* Original C code: https://gist.github.com/tommyettinger/46a874533244883189143505d203312c - uint32_t x; // The state can be seeded with any value. - uint32_t next(void) { // Call next() to get 32 pseudo-random bits, call it again to get more bits. - uint32_t z = (x += 0x6D2B79F5UL); - z = (z ^ (z >> 15)) * (z | 1UL); - z ^= z + (z ^ (z >> 7)) * (z | 61UL); - return z ^ (z >> 14); - } -*/ - random() { - /* From https://github.com/bryc/code/blob/master/jshash/PRNGs.md (seems he changed it from the original code) - this.state |= 0 - this.state = this.state + 0x6D2B79F5 | 0 - let t = Math.imul(this.state ^ this.state >>> 15, 1 | this.state) - t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t - return ((t ^ t >>> 14) >>> 0) / 4294967296 - */ - // From https://github.com/michaeldzjap/rand-seed/blob/develop/src/Algorithms/Mulberry32.ts (exact copy of original) - // >>> is a bitshift which returns an unsigned 32-bit integer - let z = (this.state += 0x6D2B79F5) - z = Math.imul(z ^ (z >>> 15), z | 1) - z ^= z + Math.imul(z ^ (z >>> 7), z | 61) - return ((z ^ (z >>> 14)) >>> 0) / 0x100000000 // dividing the integer by 0x100000000 makes it a float from 0 to 1 - } - importState(state) { - this.state = state >>> 0 - } - exportState() { - return this.state - } -} diff --git a/private/pluggablePrng.js b/private/pluggablePrng.js deleted file mode 100644 index 850bc3b..0000000 --- a/private/pluggablePrng.js +++ /dev/null @@ -1,79 +0,0 @@ -/* -does not provide cryptographically secure random numbers -*/ - -export class PluggablePRNG { - #SeedGenerator; #RandomGenerator; #initialState; #random - /** - * @param {Object} options An object with the options to use. - * @param {*} [options.seed] The seed to initialize the PRNG with. If used together with a `SeedGenerator` it can usually be any string or number which the `SeedGenerator` will convert into the required format. - * @param {*} options.RandomGenerator A class implementing the PRNG algorithm to use. - * @param {*} [options.SeedGenerator] If the PRNG needs the seed to go through a special algorithm then supply a SeedGenerator class here. - */ - constructor({seed = undefined, RandomGenerator, SeedGenerator = undefined}) { - this.#SeedGenerator = SeedGenerator - this.#RandomGenerator = RandomGenerator - - const seedGenerator = this.#SeedGenerator ? new this.#SeedGenerator(seed) : undefined - const randomGenerator = new this.#RandomGenerator(seedGenerator ? seedGenerator.seed() : seed) - this.#initialState = randomGenerator.exportState() - - // randomGenerator.random must behave just like Math.random - this.#random = randomGenerator.random.bind(randomGenerator) - this.exportState = randomGenerator.exportState.bind(randomGenerator) - this.importState = randomGenerator.importState.bind(randomGenerator) - } - /** - * This function allows you to change the seed without having to create a new PluggablePRNG instance. - * @param {*} seed - */ - changeSeed(seed) { - const seedGenerator = this.#SeedGenerator ? new this.#SeedGenerator(seed) : undefined - const randomGenerator = new this.#RandomGenerator(seedGenerator ? seedGenerator.seed() : seed) - this.importState(randomGenerator.exportState()) - } - /** - * Get a random integer. Optionally contrain it to a certain size by providing a `max` or a `min, max`. - * @param {number} [minOrMax] The minimum value OR the maximum value if used alone. - * @param {number} [max] The maximum value. - * @returns {number} An integer. - */ - randomInteger(minOrMax, max) { - switch (arguments.length) { - case 0: return this.#random() * 0x100000000 // 2^32 (makes an int from a 0 to 1 float) - case 1: max = minOrMax; minOrMax = 0 - } - return Math.floor(this.#random() * (max-minOrMax+1)) + minOrMax - } - /** - * Get a random float. Optionally contrain it to a certain size by providing a `max` or a `min, max`. - * @param {number} [minOrMax] The minimum value OR the maximum value if used alone. - * @param {number} [max] The maximum value. - * @returns {number} A float. - */ - randomFloat(minOrMax, max) { - switch (arguments.length) { // x | 0 explained: https://stackoverflow.com/a/7488075/4216153 - case 0: return this.#random() + (this.#random() * 0x200000 | 0) * 1.1102230246251565e-16 // 2^-53 - case 1: max = minOrMax; minOrMax = 0 - } - return this.#random() * (max-minOrMax) + minOrMax - } - /** - * Reset the PRNG to its initial state. It "rewinds" it back to start so that it can be used to generate the same numbers again. - */ - reset() { - this.importState(this.#initialState) - } - /** - * Skip the generator ahead - * @param {*} numbersToSkip - */ - skipAhead(numbersToSkip=1) { - for (let i=0; i>> 19 - } - return function () { - h = Math.imul(h ^ h >>> 16, 2246822507) - h = Math.imul(h ^ h >>> 13, 3266489909) - return (h ^= h >>> 16) >>> 0 - } - } - */ - constructor(seed) { - const seedString = seed.toString() - this.state = 1779033703 ^ seedString.length - for (let i=0; i < seedString.length; i++) { - this.state = Math.imul(this.state ^ seedString.charCodeAt(i), 3432918353) - this.state = this.state << 13 | this.state >>> 19 - } - } - seed() { - this.state = Math.imul(this.state ^ this.state >>> 16, 2246822507) - this.state = Math.imul(this.state ^ this.state >>> 13, 3266489909) - return (this.state ^= this.state >>> 16) >>> 0 - } -}