UNPKG

mgraph.random

Version:

Operation with seeded random numbers for mgraph.*

2 lines (1 loc) 1.47 kB
!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).mgraphRandom=e()}(this,(function(){"use strict";class t{constructor(t){this.seed=t}nextDouble(){let t=this.seed;return t=t+2127912214+(t<<12)>>>0,t=(3345072700^t^t>>>19)>>>0,t=t+374761393+(t<<5)>>>0,t=(t+3550635116^t<<9)>>>0,t=t+4251993797+(t<<3)>>>0,t=(3042594569^t^t>>>16)>>>0,this.seed=t,(268435455&t)/268435456}next(t){return Math.floor(this.nextDouble()*t)}uniform(){return this.nextDouble()}gaussian(){let t,e,n;do{t=2*this.nextDouble()-1,e=2*this.nextDouble()-1,n=t*t+e*e}while(n>=1||0===n);return t*Math.sqrt(-2*Math.log(n)/n)}levy(){const t=1.5,n=Math.pow(e(2.5)*Math.sin(Math.PI*t/2)/(e(1.25)*t*Math.pow(2,.25)),1/t);return this.gaussian()*n/Math.pow(Math.abs(this.gaussian()),1/t)}}function e(t){return Math.sqrt(2*Math.PI/t)*Math.pow(1/Math.E*(t+1/(12*t-1/(10*t))),t)}function n(e){const n="number"==typeof e?e:Date.now();return new t(n)}return Object.assign(n,{random:n,randomIterator:function(t,e){const o=e||n();if("function"!=typeof o.next)throw new Error("customRandom does not match expected API: next() function is missing");return{forEach(e){const n=t;for(let t=n.length-1;t>0;t--){const s=o.next(t+1),r=n[s];n[s]=n[t],n[t]=r,e(r)}n.length&&e(n[0])},shuffle(){const e=t;for(let t=e.length-1;t>0;t--){const n=o.next(t+1);[e[t],e[n]]=[e[n],e[t]]}return e}}}})}));