UNPKG

ts-useful

Version:

Functions for animation, color transitions, ecliptic, bezier, decasteljau, curves, three dimensional curves, smooth scrolling, random range, randomItem, mobius index, vectors, physics vectors, and easing.

121 lines 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WeylRng = void 0; /** * copied almost directly from Mersenne Twister implementation found in https://gist.github.com/banksean/300494 * all rights reserved to them. */ class WeylRng { constructor(seed) { /* least significant r bits */ this.mt = new Array(WeylRng.N); /* the array for the state vector */ this.mti = WeylRng.N + 1; this.seed = seed ? seed : new Date().getTime(); this.init_genrand(this.seed); } get Seed() { return this.seed; } init_genrand(s) { this.mt[0] = s >>> 0; for (this.mti = 1; this.mti < WeylRng.N; this.mti++) { var s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30); this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + this.mti; /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ /* In the previous versions, MSBs of the seed affect */ /* only MSBs of the array mt[]. */ /* 2002/01/09 modified by Makoto Matsumoto */ this.mt[this.mti] >>>= 0; /* for >32 bit machines */ } } /** * generates a random number on [0,0xffffffff]-interval * @private */ _nextInt32() { var y; var mag01 = new Array(0x0, WeylRng.MATRIX_A); /* mag01[x] = x * MATRIX_A for x=0,1 */ if (this.mti >= WeylRng.N) { /* generate N words at one time */ var kk; if (this.mti == WeylRng.N + 1) /* if init_genrand() has not been called, */ this.init_genrand(5489); /* a default initial seed is used */ for (kk = 0; kk < WeylRng.N - WeylRng.M; kk++) { y = (this.mt[kk] & WeylRng.UPPER_MASK) | (this.mt[kk + 1] & WeylRng.LOWER_MASK); this.mt[kk] = this.mt[kk + WeylRng.M] ^ (y >>> 1) ^ mag01[y & 0x1]; } for (; kk < WeylRng.N - 1; kk++) { y = (this.mt[kk] & WeylRng.UPPER_MASK) | (this.mt[kk + 1] & WeylRng.LOWER_MASK); this.mt[kk] = this.mt[kk + (WeylRng.M - WeylRng.N)] ^ (y >>> 1) ^ mag01[y & 0x1]; } y = (this.mt[WeylRng.N - 1] & WeylRng.UPPER_MASK) | (this.mt[0] & WeylRng.LOWER_MASK); this.mt[WeylRng.N - 1] = this.mt[WeylRng.M - 1] ^ (y >>> 1) ^ mag01[y & 0x1]; this.mti = 0; } y = this.mt[this.mti++]; /* Tempering */ y ^= (y >>> 11); y ^= (y << 7) & 0x9d2c5680; y ^= (y << 15) & 0xefc60000; y ^= (y >>> 18); return y >>> 0; } /** * generates an int32 pseudo random number * @param range: an optional [from, to] range, if not specified the result will be in range [0,0xffffffff] * @return {number} */ nextInt32(range) { var result = this._nextInt32(); if (!range) { return result; } return (result % (range[1] - range[0])) + range[0]; } /** * generates a random number on [0,0x7fffffff]-interval */ nextInt31() { return (this._nextInt32() >>> 1); } /** * generates a random number on [0,1]-real-interval */ nextNumber() { return this._nextInt32() * (1.0 / 4294967295.0); } /** * generates a random number on [0,1) with 53-bit resolution */ nextNumber53() { var a = this._nextInt32() >>> 5, b = this._nextInt32() >>> 6; return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); } Shuffle(array) { if (array.length === 0) return []; let position = array.length; let i = 0; let t = undefined; while (position > 0) { i = Math.floor(this.nextNumber() * position--); t = array[position]; array[position] = array[i]; array[i] = t; } return array; } } exports.WeylRng = WeylRng; WeylRng.N = 624; WeylRng.M = 397; WeylRng.MATRIX_A = 0x9908b0df; /* constant vector a */ WeylRng.UPPER_MASK = 0x80000000; /* most significant w-r bits */ WeylRng.LOWER_MASK = 0x7fffffff; //# sourceMappingURL=weylRnd.js.map