@grandom/engines-umd
Version:
Various UMD builds of RNG engines for the grandom library.
24 lines (23 loc) • 7.65 kB
JavaScript
/*!
* @grandom/engines v4.0.1
* https://github.com/grandom-library/grandom-js/tree/main/packages/engines#readme
*
* Copyright (c) 2023 Richard King <richrdkng@gmail.com> (www.richrdkng.com)
* Released under the MIT License
* https://github.com/grandom-library/grandom-js/tree/main/packages/engines#readme/blob/main/LICENSE
*
* Date: 2023-09-06T22:13:45.482Z
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).grandomEnginesSeeded=e()}(this,(function(){"use strict";function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}let e=class t{constructor(t){this._name=t}_next(){throw new Error("_next() must be implemented.")}get name(){return this._name}nextBoolean(){return this._next()<.5}nextInteger(e=t.DEFAULT_INTEGER_MINIMUM,s=t.DEFAULT_INTEGER_MAXIMUM,n=t.DEFAULT_INCLUDE_MINIMUM,r=t.DEFAULT_INCLUDE_MAXIMUM){let i=0,_=0;return n&&!r?(i=0,_=0):n&&r?_=1:!n&&r?i=1:(i=1,_=-1),Math.floor(this._next()*(s-e+_))+e+i}nextFloat(e=t.DEFAULT_FLOAT_MINIMUM,s=t.DEFAULT_FLOAT_MAXIMUM,n=t.DEFAULT_INCLUDE_MINIMUM,r=t.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(s-e)+e;if(n&&!r)return i;{const t=i+Number.EPSILON*i;return n&&r?t<s?t:s:!n&&r?t>e?t:e+Number.EPSILON:t<=e?e+Number.EPSILON:t>=s?s-Number.EPSILON:t}}nextBigInt(e=t.DEFAULT_BIGINT_MINIMUM,s=t.DEFAULT_BIGINT_MAXIMUM,n=t.DEFAULT_INCLUDE_MINIMUM,r=t.DEFAULT_INCLUDE_MAXIMUM){let i=0n,_=0n;return n&&!r?(i=0n,_=0n):n&&r?_=1n:!n&&r?i=1n:(i=1n,_=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(s)-BigInt(e)+_)/BigInt(Number.MAX_SAFE_INTEGER)+BigInt(e)+i}nextString(t,e){const s=e.length;let n="";for(let r=0;r<t;r++)n+=e.charAt(Math.floor(this._next()*s));return n}nextWeighted(t,e){let s=0;for(let t=0;t<e.length;t++)s+=e[t];const n=this._next()*s;let r=0;for(let s=0;s<t.length;s++)if(r+=e[s],r>n)return t[s];return t[t.length-1]}pickArray(t){return t[Math.floor(this._next()*t.length)]}shuffleArray(t){let e,s;for(let n=t.length-1;n>0;n--)e=Math.floor(this._next()*(n+1)),s=t[n],t[n]=t[e],t[e]=s}};e.DEFAULT_INCLUDE_MINIMUM=!0,e.DEFAULT_INCLUDE_MAXIMUM=!1,e.DEFAULT_FLOAT_MINIMUM=0,e.DEFAULT_FLOAT_MAXIMUM=1,e.DEFAULT_INTEGER_MINIMUM=0,e.DEFAULT_INTEGER_MAXIMUM=4294967296,e.DEFAULT_BIGINT_MINIMUM=0n,e.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var s=e;var n=t(class{constructor(t,e){let s,n;void 0!==t&&(("number"==typeof t||Array.isArray(t))&&(s=t),null!==t&&"object"==typeof t?n=t:null!==e&&"object"==typeof e&&(n=e)),void 0===s&&(s="function"==typeof n?.autoSeeder?n.autoSeeder():Math.floor(Math.random()*(new Date).getTime())),this._N=n?.stateLength??624,this._M=n?.statePeriod??397,this._MATRIX_A=n?.matrixA??2567483615,this._UPPER_MASK=n?.upperMask??2147483648,this._LOWER_MASK=n?.lowerMask??2147483647,this._stateVector=new Array(this._N),this._stateIndex=this._N+1,this._init(s)}get seed(){return this._seed}set seed(t){this._init(t)}get stateLength(){return this._N}get statePeriod(){return this._M}get matrixA(){return this._MATRIX_A}get upperMask(){return this._UPPER_MASK}get lowerMask(){return this._LOWER_MASK}randomInt32(){let t;const e=[0,this._MATRIX_A];if(this._stateIndex>=this._N){let s;for(s=0;s<this._N-this._M;s++)t=this._stateVector[s]&this._UPPER_MASK|this._stateVector[s+1]&this._LOWER_MASK,this._stateVector[s]=this._stateVector[s+this._M]^t>>>1^e[1&t];for(;s<this._N-1;s++)t=this._stateVector[s]&this._UPPER_MASK|this._stateVector[s+1]&this._LOWER_MASK,this._stateVector[s]=this._stateVector[s+(this._M-this._N)]^t>>>1^e[1&t];t=this._stateVector[this._N-1]&this._UPPER_MASK|this._stateVector[0]&this._LOWER_MASK,this._stateVector[this._N-1]=this._stateVector[this._M-1]^t>>>1^e[1&t],this._stateIndex=0}return t=this._stateVector[this._stateIndex++],t^=t>>>11,t^=t<<7&2636928640,t^=t<<15&4022730752,t^=t>>>18,t>>>0}randomInt31(){return this.randomInt32()>>>1}randomFloat1(){return this.randomInt32()*(1/4294967295)}randomFloat2(){return this.randomInt32()*(1/4294967296)}randomFloat3(){return(this.randomInt32()+.5)*(1/4294967296)}randomFloatRes53(){return(67108864*(this.randomInt32()>>>5)+(this.randomInt32()>>>6))*(1/9007199254740992)}_init(t){this._seed=t,Array.isArray(t)?this._initWithArray(t):this._initWithNumber(t)}_initWithNumber(t){for(this._stateVector[0]=t>>>0,this._stateIndex=1;this._stateIndex<this._N;this._stateIndex++){const t=this._stateVector[this._stateIndex-1]^this._stateVector[this._stateIndex-1]>>>30;this._stateVector[this._stateIndex]=(1812433253*((4294901760&t)>>>16)<<16)+1812433253*(65535&t)+this._stateIndex,this._stateVector[this._stateIndex]>>>=0}}_initWithArray(t){let e=1,s=0,n=this._N>t.length?this._N:t.length;for(this._initWithNumber(19650218);n;n--){const n=this._stateVector[e-1]^this._stateVector[e-1]>>>30;this._stateVector[e]=(this._stateVector[e]^(1664525*((4294901760&n)>>>16)<<16)+1664525*(65535&n))+t[s]+s,this._stateVector[e]>>>=0,e++,s++,e>=this._N&&(this._stateVector[0]=this._stateVector[this._N-1],e=1),s>=t.length&&(s=0)}for(n=this._N-1;n;n--){const t=this._stateVector[e-1]^this._stateVector[e-1]>>>30;this._stateVector[e]=(this._stateVector[e]^(1566083941*((4294901760&t)>>>16)<<16)+1566083941*(65535&t))-e,this._stateVector[e]>>>=0,e++,e>=this._N&&(this._stateVector[0]=this._stateVector[this._N-1],e=1)}this._stateVector[0]=2147483648}});
/*!
* @grandom/fnv1a v1.0.3
* https://github.com/grandom-library/fnv1a#readme
*
* Copyright (c) 2023 Richard King <richrdkng@gmail.com> (www.richrdkng.com)
* Released under the MIT License
* https://github.com/grandom-library/fnv1a#readme/blob/main/LICENSE
*
* Date: 2023-08-28T07:39:33.259Z
*/
const r=2166136261,i={32:BigInt(16777619),64:BigInt(1099511628211),128:BigInt(2)**BigInt(88)+BigInt(315),256:BigInt(2)**BigInt(168)+BigInt(355),512:BigInt(2)**BigInt(344)+BigInt(343),1024:BigInt(2)**BigInt(680)+BigInt(397)},_={32:BigInt(r),64:14695981039346656037n,128:144066263297769815596495629667062367629n,256:100029257958052580907070968620625704837092796014241193945225284501741471925557n,512:9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785n,1024:14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915n};var o=t((function(t,e){if("string"!=typeof t)throw new TypeError(`The 'string' argument must be a string, got: ${t} (typeof === '${typeof t}').`);const s=t.length;if(void 0===e){let e=r;for(let n=0;n<s;n++)e^=t.charCodeAt(n),e+=(e<<1)+(e<<4)+(e<<7)+(e<<8)+(e<<24);return e>>>0}if("number"!=typeof e)throw new TypeError(`The 'hashSize' argument must be a number, got: ${e} (typeof === '${typeof e}').`);if(!(e in i))throw new RangeError(`The 'hashSize' argument must be one of 32, 64, 128, 256, 512, 1024, got: ${e}.`);const n=e,o=i[n];let h=_[n];for(let e=0;e<s;e++)h^=BigInt(t.charCodeAt(e)),h=BigInt.asUintN(n,h*o);return h}));return class extends s{constructor(){super("mt19937"),this._engine=new n,this.seed=Math.floor(Math.random()*(new Date).getTime())}get seed(){return this._seed}set seed(t){if("number"==typeof t)this._engine.seed=t,this._seed=t;else{if("string"!=typeof t)throw new TypeError(`Seed must be a number, or a string, got: ${t} (typeof === '${typeof t}').`);this._engine.seed=o(t),this._seed=t}}_next(){return this._engine.randomFloat2()}}}));
//# sourceMappingURL=min.seeded.js.map