@grandom/boolean-umd
Version:
Various UMD builds of a great random boolean generator.
13 lines (12 loc) • 3.67 kB
JavaScript
/*!
* @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.375Z
*/
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n(require("crypto")):"function"==typeof define&&define.amd?define(["crypto"],n):(e="undefined"!=typeof globalThis?globalThis:e||self).grandomBooleanCrypto=n(e.require$$1)}(this,(function(e){"use strict";function n(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function t(e){throw new Error('Could not dynamically require "'+e+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var r={};class o{constructor(e){this._name=e}_next(){throw new Error("_next() must be implemented.")}get name(){return this._name}nextBoolean(){return this._next()<.5}nextInteger(e=o.DEFAULT_INTEGER_MINIMUM,n=o.DEFAULT_INTEGER_MAXIMUM,t=o.DEFAULT_INCLUDE_MINIMUM,r=o.DEFAULT_INCLUDE_MAXIMUM){let i=0,M=0;return t&&!r?(i=0,M=0):t&&r?M=1:!t&&r?i=1:(i=1,M=-1),Math.floor(this._next()*(n-e+M))+e+i}nextFloat(e=o.DEFAULT_FLOAT_MINIMUM,n=o.DEFAULT_FLOAT_MAXIMUM,t=o.DEFAULT_INCLUDE_MINIMUM,r=o.DEFAULT_INCLUDE_MAXIMUM){const i=this._next()*(n-e)+e;if(t&&!r)return i;{const o=i+Number.EPSILON*i;return t&&r?o<n?o:n:!t&&r?o>e?o:e+Number.EPSILON:o<=e?e+Number.EPSILON:o>=n?n-Number.EPSILON:o}}nextBigInt(e=o.DEFAULT_BIGINT_MINIMUM,n=o.DEFAULT_BIGINT_MAXIMUM,t=o.DEFAULT_INCLUDE_MINIMUM,r=o.DEFAULT_INCLUDE_MAXIMUM){let i=0n,M=0n;return t&&!r?(i=0n,M=0n):t&&r?M=1n:!t&&r?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(e,n){const t=n.length;let r="";for(let o=0;o<e;o++)r+=n.charAt(Math.floor(this._next()*t));return r}nextWeighted(e,n){let t=0;for(let e=0;e<n.length;e++)t+=n[e];const r=this._next()*t;let o=0;for(let t=0;t<e.length;t++)if(o+=n[t],o>r)return e[t];return e[e.length-1]}pickArray(e){return e[Math.floor(this._next()*e.length)]}shuffleArray(e){let n,t;for(let r=e.length-1;r>0;r--)n=Math.floor(this._next()*(r+1)),t=e[r],e[r]=e[n],e[n]=t}}o.DEFAULT_INCLUDE_MINIMUM=!0,o.DEFAULT_INCLUDE_MAXIMUM=!1,o.DEFAULT_FLOAT_MINIMUM=0,o.DEFAULT_FLOAT_MAXIMUM=1,o.DEFAULT_INTEGER_MINIMUM=0,o.DEFAULT_INTEGER_MAXIMUM=4294967296,o.DEFAULT_BIGINT_MINIMUM=0n,o.DEFAULT_BIGINT_MAXIMUM=18446744073709551616n;var i=o;var M=i,a=class{constructor(e){this._engine=e}get engine(){return this._engine}};r.RandomEngine=M;var s=r.RandomGenerator=a,I=r;class _ extends I.RandomEngine{constructor(){if(super("crypto"),"undefined"!=typeof window&&window.crypto&&window.crypto.getRandomValues)this._next=()=>{const e=new Uint8Array(4);return window.crypto.getRandomValues(e),Math.abs((e[0]<<24|e[1]<<16|e[2]<<8|e[3])/4294967296)};else{if(void 0===t)throw new Error("Secure random number generation is not supported in this environment.");{const n=e;this._next=()=>n.randomBytes(4).readUInt32BE(0)/4294967296}}}}var l=n(_);class u extends s{boolean(e){if(void 0!==e){if("number"!=typeof e)throw new TypeError(`bias must be a number, got: ${typeof e}.`);if(e==e)return e<0?e=0:e>1&&(e=1),this._engine.nextFloat()<e}return this._engine.nextBoolean()}}const E=new u(new l),U=E.boolean.bind(E);return Object.defineProperties(U,{CryptoEngine:{value:l},RandomBoolean:{value:u}}),U}));
//# sourceMappingURL=crypto.min.js.map