UNPKG

@grandom/boolean-umd

Version:
33 lines (32 loc) 8.52 kB
/*! * @grandom/boolean v1.4.7 * https://github.com/grandom-library/grandom-js/tree/main/packages/boolean#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/boolean#readme/blob/main/LICENSE * * Date: 2023-09-06T22:04:47.372Z */ !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).grandomBooleanSeeded=e()}(this,(function(){"use strict";function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var e={};class n{constructor(t){this._name=t}_next(){throw new Error("_next() must be implemented.")}get name(){return this._name}nextBoolean(){return this._next()<.5}nextInteger(t=n.DEFAULT_INTEGER_MINIMUM,e=n.DEFAULT_INTEGER_MAXIMUM,s=n.DEFAULT_INCLUDE_MINIMUM,r=n.DEFAULT_INCLUDE_MAXIMUM){let i=0,o=0;return s&&!r?(i=0,o=0):s&&r?o=1:!s&&r?i=1:(i=1,o=-1),Math.floor(this._next()*(e-t+o))+t+i}nextFloat(t=n.DEFAULT_FLOAT_MINIMUM,e=n.DEFAULT_FLOAT_MAXIMUM,s=n.DEFAULT_INCLUDE_MINIMUM,r=n.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(e-t)+t;if(s&&!r)return i;{const n=i+Number.EPSILON*i;return s&&r?n<e?n:e:!s&&r?n>t?n:t+Number.EPSILON:n<=t?t+Number.EPSILON:n>=e?e-Number.EPSILON:n}}nextBigInt(t=n.DEFAULT_BIGINT_MINIMUM,e=n.DEFAULT_BIGINT_MAXIMUM,s=n.DEFAULT_INCLUDE_MINIMUM,r=n.DEFAULT_INCLUDE_MAXIMUM){let i=0n,o=0n;return s&&!r?(i=0n,o=0n):s&&r?o=1n:!s&&r?i=1n:(i=1n,o=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(e)-BigInt(t)+o)/BigInt(Number.MAX_SAFE_INTEGER)+BigInt(t)+i}nextString(t,e){const n=e.length;let s="";for(let r=0;r<t;r++)s+=e.charAt(Math.floor(this._next()*n));return s}nextWeighted(t,e){let n=0;for(let t=0;t<e.length;t++)n+=e[t];const s=this._next()*n;let r=0;for(let n=0;n<t.length;n++)if(r+=e[n],r>s)return t[n];return t[t.length-1]}pickArray(t){return t[Math.floor(this._next()*t.length)]}shuffleArray(t){let e,n;for(let s=t.length-1;s>0;s--)e=Math.floor(this._next()*(s+1)),n=t[s],t[s]=t[e],t[e]=n}}n.DEFAULT_INCLUDE_MINIMUM=!0,n.DEFAULT_INCLUDE_MAXIMUM=!1,n.DEFAULT_FLOAT_MINIMUM=0,n.DEFAULT_FLOAT_MAXIMUM=1,n.DEFAULT_INTEGER_MINIMUM=0,n.DEFAULT_INTEGER_MAXIMUM=4294967296,n.DEFAULT_BIGINT_MINIMUM=0n,n.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var s=n;var r=class{constructor(t){this._engine=t}get engine(){return this._engine}},i=s,o=r;e.RandomEngine=i;var _=e.RandomGenerator=o; /*! * @grandom/mt19937 v1.0.2 * https://github.com/grandom-library/mt19937#readme * * Copyright (c) 2023 Richard King <richrdkng@gmail.com> (www.richrdkng.com) * Released under the BSD-3-Clause License * https://github.com/grandom-library/mt19937#readme/blob/main/LICENSE * * Date: 2023-08-29T07:37:31.671Z */var h=class{constructor(t,e){let n,s;void 0!==t&&(("number"==typeof t||Array.isArray(t))&&(n=t),null!==t&&"object"==typeof t?s=t:null!==e&&"object"==typeof e&&(s=e)),void 0===n&&(n="function"==typeof s?.autoSeeder?s.autoSeeder():Math.floor(Math.random()*(new Date).getTime())),this._N=s?.stateLength??624,this._M=s?.statePeriod??397,this._MATRIX_A=s?.matrixA??2567483615,this._UPPER_MASK=s?.upperMask??2147483648,this._LOWER_MASK=s?.lowerMask??2147483647,this._stateVector=new Array(this._N),this._stateIndex=this._N+1,this._init(n)}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 n;for(n=0;n<this._N-this._M;n++)t=this._stateVector[n]&this._UPPER_MASK|this._stateVector[n+1]&this._LOWER_MASK,this._stateVector[n]=this._stateVector[n+this._M]^t>>>1^e[1&t];for(;n<this._N-1;n++)t=this._stateVector[n]&this._UPPER_MASK|this._stateVector[n+1]&this._LOWER_MASK,this._stateVector[n]=this._stateVector[n+(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,n=0,s=this._N>t.length?this._N:t.length;for(this._initWithNumber(19650218);s;s--){const s=this._stateVector[e-1]^this._stateVector[e-1]>>>30;this._stateVector[e]=(this._stateVector[e]^(1664525*((4294901760&s)>>>16)<<16)+1664525*(65535&s))+t[n]+n,this._stateVector[e]>>>=0,e++,n++,e>=this._N&&(this._stateVector[0]=this._stateVector[this._N-1],e=1),n>=t.length&&(n=0)}for(s=this._N-1;s;s--){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 a=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)},M={32:BigInt(a),64:14695981039346656037n,128:144066263297769815596495629667062367629n,256:100029257958052580907070968620625704837092796014241193945225284501741471925557n,512:9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785n,1024:14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915n};var u=e,d=h,g=function(t,e){if("string"!=typeof t)throw new TypeError(`The 'string' argument must be a string, got: ${t} (typeof === '${typeof t}').`);const n=t.length;if(void 0===e){let e=a;for(let s=0;s<n;s++)e^=t.charCodeAt(s),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 s=e,r=I[s];let i=M[s];for(let e=0;e<n;e++)i^=BigInt(t.charCodeAt(e)),i=BigInt.asUintN(s,i*r);return i};class c extends u.RandomEngine{constructor(){super("mt19937"),this._engine=new d,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=g(t),this._seed=t}}_next(){return this._engine.randomFloat2()}}var l=t(c);class f extends _{boolean(t){if(void 0!==t){if("number"!=typeof t)throw new TypeError(`bias must be a number, got: ${typeof t}.`);if(t==t)return t<0?t=0:t>1&&(t=1),this._engine.nextFloat()<t}return this._engine.nextBoolean()}}const A=new l,E=new f(A),N=E.boolean.bind(E);return Object.defineProperties(N,{SeededEngine:{value:l},RandomBoolean:{value:f},seed:{get:()=>A.seed,set:t=>{A.seed=t}}}),N})); //# sourceMappingURL=seeded.min.js.map