UNPKG

@grandom/engines-umd

Version:
13 lines (12 loc) 3.05 kB
/*! * @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.489Z */ !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).grandomEnginesCrypto=e()}(this,(function(){"use strict";let t=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,n=t.DEFAULT_INTEGER_MAXIMUM,r=t.DEFAULT_INCLUDE_MINIMUM,o=t.DEFAULT_INCLUDE_MAXIMUM){let i=0,M=0;return r&&!o?(i=0,M=0):r&&o?M=1:!r&&o?i=1:(i=1,M=-1),Math.floor(this._next()*(n-e+M))+e+i}nextFloat(e=t.DEFAULT_FLOAT_MINIMUM,n=t.DEFAULT_FLOAT_MAXIMUM,r=t.DEFAULT_INCLUDE_MINIMUM,o=t.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(n-e)+e;if(r&&!o)return i;{const t=i+Number.EPSILON*i;return r&&o?t<n?t:n:!r&&o?t>e?t:e+Number.EPSILON:t<=e?e+Number.EPSILON:t>=n?n-Number.EPSILON:t}}nextBigInt(e=t.DEFAULT_BIGINT_MINIMUM,n=t.DEFAULT_BIGINT_MAXIMUM,r=t.DEFAULT_INCLUDE_MINIMUM,o=t.DEFAULT_INCLUDE_MAXIMUM){let i=0n,M=0n;return r&&!o?(i=0n,M=0n):r&&o?M=1n:!r&&o?i=1n:(i=1n,M=-1n),BigInt(Math.floor(this._next()*Number.MAX_SAFE_INTEGER))*(BigInt(n)-BigInt(e)+M)/BigInt(Number.MAX_SAFE_INTEGER)+BigInt(e)+i}nextString(t,e){const n=e.length;let r="";for(let o=0;o<t;o++)r+=e.charAt(Math.floor(this._next()*n));return r}nextWeighted(t,e){let n=0;for(let t=0;t<e.length;t++)n+=e[t];const r=this._next()*n;let o=0;for(let n=0;n<t.length;n++)if(o+=e[n],o>r)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 r=t.length-1;r>0;r--)e=Math.floor(this._next()*(r+1)),n=t[r],t[r]=t[e],t[e]=n}};t.DEFAULT_INCLUDE_MINIMUM=!0,t.DEFAULT_INCLUDE_MAXIMUM=!1,t.DEFAULT_FLOAT_MINIMUM=0,t.DEFAULT_FLOAT_MAXIMUM=1,t.DEFAULT_INTEGER_MINIMUM=0,t.DEFAULT_INTEGER_MAXIMUM=4294967296,t.DEFAULT_BIGINT_MINIMUM=0n,t.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var e=t;return class extends e{constructor(){if(super("cross-crypto"),this._isBrowserEnvironment())this._next=this._initBrowserCrypto();else{if(!this._isNodeJSEnvironment())throw new Error("Secure random number generation is not supported in this environment.");this._next=this._initNodeJSCrypto()}}_isBrowserEnvironment(){return"undefined"!=typeof window&&"getRandomValues"in window?.crypto}_isNodeJSEnvironment(){return"undefined"!=typeof require}_initBrowserCrypto(){return()=>{const t=new Uint8Array(4);return window.crypto.getRandomValues(t),Math.abs((t[0]<<24|t[1]<<16|t[2]<<8|t[3])/4294967296)}}_initNodeJSCrypto(){const t=require("crypto");return()=>t.randomBytes(4).readUInt32BE(0)/4294967296}}})); //# sourceMappingURL=min.crypto.js.map