UNPKG

@creatartis/randomness

Version:

Pseudo-random number generation functions and utilities.

2 lines 4.32 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.randomness=t():e.randomness=t()}("undefined"!=typeof self?self:this,(function(){return(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{LinearCongruential:()=>u,MersenneTwister:()=>h,Randomness:()=>l,SERMAT:()=>m,id:()=>r});const r="randomness";let n=null;class i{constructor(e){if("function"==typeof e)Object.defineProperty(this,"generator",{value:e});else if(void 0!==e)throw new TypeError(`Unsupported random number generator ${e}!`)}generator(){return Math.random()}static get DEFAULT(){return n||(n=new i),n}random(e=NaN,t=NaN){const r=this.generator();return Number.isNaN(t)?Number.isNaN(e)?r:r*e:(1-r)*e+r*t}randomInt(e=NaN,t=NaN){let r=e,n=t;return Number.isNaN(r)&&Number.isNaN(n)&&(r=0,n=100),Math.floor(this.random(r,n))}randomBool(e=.5){return this.random()<+e}*randoms(e,t,r){const n=+e;for(let e=0;e<n;e+=1)yield this.random(t,r)}choice(e){return e.length<1?void 0:e[this.randomInt(e.length)]}choices(e,t){return this.split(e,t)[0]}split(e,t){const r=[...t];let n=[];for(let t=Math.max(e,0);r.length>0&&t>0;t-=1){const e=this.randomInt(r.length),t=r.splice(e,1);n=n.concat(t)}return[n,r]}shuffle(e){return this.choices(e.length,e)}static normalizeWeights(e){let t=0;for(const[,r]of e.entries()){if(Number.isNaN(r)||r<0)throw new Error(`Cannot normalize with weight ${r}!`);t+=r}const r=new Map,{size:n}=e;for(const[i,o]of e.entries())r.set(i,0===t?1/n:o/t);return r}weightedChoice(e,t){let r=this.random();for(const[t,n]of e.entries())if(r-=n,r<=1e-15)return t;if(arguments.length<2)throw new Error("Weighted choice failed. Are weights normalized?");return t}*weightedChoices(e,t){if(e>0){const r=new Map(t);let n,i=1;for(let t=0;t<e;t+=1){n=this.random(i);for(const[e,t]of r.entries())n-=t,n<=0&&(i-=t,r.delete(e),yield e)}}}averagedDistribution(e){const t=Math.max(+e,2);return new i((()=>{let r=0;for(let e=0;e<t;e+=1)r+=this.generator();return r/e}))}}var o,s,a;a={identifier:`${r}.Randomness`,serializer:e=>e.generator===i.prototype.generator?[]:[e.generator],materializer:(e,t)=>t&&(t.length<1?new i:new i(t[0]))},(s="__SERMAT__")in(o=i)?Object.defineProperty(o,s,{value:a,enumerable:!0,configurable:!0,writable:!0}):o[s]=a;const l=i;class c extends l{constructor(e,t,r,n){const i=Number.isNaN(n)?Date.now():Math.floor(n);super(),Object.defineProperty(this,"arguments",{value:[e,t,r,n]}),this.current=i}generator(){const[e,t,r]=this.arguments;return this.current=(t*this.current+r)%e,this.current/e}static numericalRecipies(e){return new c(4294967295,1664525,1013904223,e)}static borlandC(e){return new c(4294967295,22695477,1,e)}static glibc(e){return new c(4294967295,1103515245,12345,e)}}!function(e,t,r){t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r}(c,"__SERMAT__",{identifier:`${r}.LinearCongruential`,serializer:e=>[...e.arguments],materializer:(e,t)=>t&&new c(...t)});const u=c;function d(e){return e<0?e+4294967296:e}class f extends l{constructor(e){super(),this.seed=Number.isNaN(e)?Date.now():Math.floor(e),this.numbers=f.initialize(this.seed),this.index=0}generate(){const{numbers:e}=this;0===this.index&&f.generate(e);let t=e[this.index];return t=d(t^t<<11),t=d(t^t>>>7&2636928640),t=d(t^t>>>15&4022730752),t=d(t^t<<18),this.index=(this.index+1)%624,t/4294967295}static initialize(e){const t=new Array(624);let r=e;t[0]=r;for(let e=1;e<624;e+=1)r=(1812433253*d(r^r<<30)+e)%4294967295,t[e]=r;return t}static generate(e){for(let t=0;t<624;t+=1){const r=2147483648&e[t]|2147483647&e[(t+1)%624];e[t]=d(e[(t+397)%624]^2*r),0!=(1&r)&&(e[t]=d(2567483615^e[t]))}}}!function(e,t,r){t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r}(f,"__SERMAT__",{identifier:`${r}.MersenneTwister`,serializer:e=>[e.seed],materializer:(e,t)=>t&&new f(t[0])});const h=f,m={include:[l,u,h]};return t})()})); //# sourceMappingURL=randomness.js.map