UNPKG

randomcryp

Version:

A cryptographically secure, feature rich, zero dependency and browser friendly random number generator library.

2 lines (1 loc) 1.71 kB
var N=Math.pow(2,37),R=Math.pow(2,21),D=Math.pow(2,5),L=()=>{let[z,B,H,J]=crypto.getRandomValues(new Uint16Array(4));return z*N+B*R+H*D+(J&D-1)};var Q=()=>L()%2===0;var S=Math.pow(2,53),C=()=>L()/S;var V=(z)=>{if(z.length===0)return;return z[Math.floor(C()*z.length)]};var U=(z=8,B=!1)=>{let H=crypto.getRandomValues(new Uint8Array(Math.ceil(z/2))),J=Array(H.length);for(let K=0,$=0;K<z;K++,$=Math.floor(K/2))if(K%2===0)J[K]="0123456789abcdef"[H[$]>>4];else J[K]="0123456789abcdef"[H[$]&15];if(B)return"0x"+J.join("");return J.join("")};var I=Math.pow(2,37),O=Math.pow(2,21),q=Math.pow(2,5),Z=()=>{let[z,B,H,J]=crypto.getRandomValues(new Uint16Array(4));return((J&q)<1?1:-1)*(z*I+B*O+H*q+(J&q-1))};var X=()=>Z()/Number.MAX_SAFE_INTEGER;var F=(z)=>{if(z<0||z>100)throw RangeError(`${z} is not in range [0, 100]`);return C()*100<z};var M=(z)=>{if(z<0||z>1)throw RangeError(`${z} is not in range [0, 1]`);return C()<z};var Y=(z,B)=>{if(z>B)throw RangeError("min must be less than or equal to max");return z+Math.floor(C()*(B-z+1))};var j=(z,B)=>{if(z>B)throw RangeError("min must be less than or equal to max");return z+C()*(B-z)};function T(z){let B=Array.from(z);for(let H=B.length-1;H>0;H--){let J=Math.floor(C()*(H+1)),K=B[H];B[H]=B[J],B[J]=K}return B}function k(z){if(typeof z==="string")return T(z).join("");else return T(z)}var Mz={bool:Q,boolean:Q,choice:V,float:C,hex:U,ifloat:X,percentage:F,probability:M,pick:V,randInt:Y,random:C,range:j,rangeInt:Y,safeInt:Z,shuffle:k,uSafeInt:L};export{L as uSafeInt,k as shuffle,Z as safeInt,Y as rangeInt,j as range,C as random,Y as randInt,M as probability,V as pick,F as percentage,X as ifloat,U as hex,C as float,Mz as default,V as choice,Q as boolean,Q as bool};