Created
October 17, 2022 06:15
-
-
Save wchargin/8ed39799b70741e2627d6242e0827653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $t=Math.pow(2,-32),ge=32557,Le=19605,ze=62509,Jt=22609,Qt=33103,en=63335,tn=31614,nn=5125; | |
var De=class{constructor(){this._state=new Uint16Array(4),this._dv=new DataView(this._state.buffer),this._nG=null,this._hNG=!1}setSeed(t){this._hNG=!1,this._nG=null;let l=~~((t.length-2)/2),c=[];for(let b=0;b<l;b++){let k=2+2*b;c.push(parseInt(t.slice(k,k+2),16))}let e=1690382925,d=72970470,f=qe(c,e),g=qe(c,d);this._dv.setUint32(0,f),this._dv.setUint32(4,g)}rnd(){let t=this._state,l=t[0],c=t[1],e=t[2],d=t[3],f=Qt+ge*l|0,g=en+ge*c+(Le*l+(f>>>16))|0,b=tn+ge*e+Le*c+(ze*l+(g>>>16))|0,k=nn+ge*d+(Le*e+ze*c)+(Jt*l+(b>>>16));t[0]=f,t[1]=g,t[2]=b,t[3]=k;let L=(d<<21)+((d>>2^e)<<5)+((e>>2^c)>>11),B=L>>>(d>>11)|L<<(-(d>>11)&31);return $t*(B>>>0)}uniform(t=1,l=null){return l===null&&([t,l]=[0,t]),this.rnd()*(l-t)+t}gauss(t=0,l=1){if(this._hNG){this._hNG=!1;var c=this._nG;return this._nG=null,t+l*c}else{var e=0,d=0,f=0;do e=this.rnd()*2-1,d=this.rnd()*2-1,f=e*e+d*d;while(f>=1||f===0);var g=Math.sqrt(-2*Math.log(f)/f);return this._nG=d*g,this._hNG=!0,t+l*(e*g)}}odds(t){return this.uniform()<=t}choice(t){return t[Math.floor(this.uniform(0,t.length))]}weightedChoice(t){let c=t.map(([,f])=>f).reduce((f,g)=>f+g,0)*this.uniform(),e=0;for(let f=0;f<t.length;f++){let[g,b]=t[f];if(e+=b,e>=c)return g}let[d]=t[t.length-1];return d}wc(t){return this.weightedChoice(t)}shuffle(t){let l=t.map(c=>[this.uniform(0,1),c]);return l.sort((c,e)=>c[0]<e[0]?-1:1),l.map(([,c])=>c)}winnow(t,l){let c=t.slice();for(;c.length>l;){let e=Math.floor(this.rnd()*c.length);c.splice(e,1)}return c}} | |
function qe(o,t=0){for(var d=1540483477,f=o.length,g=t^f,b=0,k;f>=4;)k=o[b]&255|(o[++b]&255)<<8|(o[++b]&255)<<16|(o[++b]&255)<<24,k=(k&65535)*d+(((k>>>16)*d&65535)<<16),k^=k>>>24,k=(k&65535)*d+(((k>>>16)*d&65535)<<16),g=(g&65535)*d+(((g>>>16)*d&65535)<<16)^k,f-=4,++b;switch(f){case 3:g^=(o[b+2]&255)<<16;case 2:g^=(o[b+1]&255)<<8;case 1:g^=o[b]&255,g=(g&65535)*d+(((g>>>16)*d&65535)<<16)}return g^=g>>>13,g=(g&65535)*d+(((g>>>16)*d&65535)<<16),g^=g>>>15,g>>>0} | |
function fe(){let o=Array(64).fill().map(()=>Math.floor(Math.random()*16).toString(16));return"0x"+o.join("")} | |
const Rng = De; | |
const randomSeed = fe; | |
const rng = new Rng(); | |
rng.setSeed(randomSeed()); | |
console.log(rng.choice(["one", "two", "three"])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment