@thi.ng/random
Advanced tools
Comparing version 1.1.15 to 1.2.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@1.1.15...@thi.ng/random@1.2.0) (2020-01-26) | ||
### Features | ||
* **random:** add randomBytes() wrapper ([c536bcd](https://github.com/thi-ng/umbrella/commit/c536bcd83c766414e349f6b82494ace9888ac2ba)) | ||
## [1.1.15](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@1.1.14...@thi.ng/random@1.1.15) (2020-01-24) | ||
@@ -8,0 +19,0 @@ |
@@ -7,4 +7,5 @@ export * from "./api"; | ||
export * from "./xsadd"; | ||
export * from "./random-bytes"; | ||
export * from "./random-id"; | ||
export * from "./weighted-random"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -7,3 +7,4 @@ export * from "./api"; | ||
export * from "./xsadd"; | ||
export * from "./random-bytes"; | ||
export * from "./random-id"; | ||
export * from "./weighted-random"; |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var checks = require('@thi.ng/checks'); | ||
var api = require('@thi.ng/api'); | ||
@@ -174,2 +175,12 @@ | ||
const randomBytes = checks.hasCrypto() | ||
? (buf) => window.crypto.getRandomValues(buf) | ||
: (buf) => { | ||
const n = buf.length; | ||
for (let i = 0; i < n; i++) { | ||
buf[i] = SYSTEM.int() & 0xff; | ||
} | ||
return buf; | ||
}; | ||
const randomID = (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz", rnd = SYSTEM) => { | ||
@@ -212,3 +223,4 @@ for (const n = syms.length; --len >= 0;) { | ||
exports.XsAdd = XsAdd; | ||
exports.randomBytes = randomBytes; | ||
exports.randomID = randomID; | ||
exports.weightedRandom = weightedRandom; |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@thi.ng/api")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/api"],e):e(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.random={}),t.thi.ng.api)}(this,(function(t,e){"use strict";class r{float(t=1){return this.int()*(1/4294967295)*t}norm(t=1){return 2*(this.int()*(1/4294967295)-.5)*t}minmax(t,e){return this.float()*(e-t)+t}gaussian(t=10,e=-.5,r=1){let n=0,s=t;for(;s-- >0;)n+=this.float(r);return n/t+e}}class n extends r{constructor(t=3737844653){super(),this.buffer=new Uint32Array([t,0])}copy(){const t=new n;return t.buffer.set(this.buffer),t}seed(t){return this.buffer.set([t,0]),this}int(){const t=this.buffer,e=1540483477,r=t[1]++*e>>>0,n=t[0]=(r^r>>24^t[0]*e>>>0)*e>>>0;return(n^n>>>13)>>>0}}const s=Math.random;class i extends r{int(){return 4294967295*s()>>>0}float(t=1){return s()*t}norm(t=1){return 2*(s()-.5)*t}}const f=new i,u=[3737844653,799659867,3827263459,1552149530];class o extends r{constructor(t=u){super(),this.buffer=new Uint32Array(4),this.seed(t)}copy(){return new o(this.buffer)}bytes(){return new Uint8Array(this.buffer.buffer)}seed(t){return this.buffer.set(t),this}int(){const t=this.buffer;let e,r=t[3];return r^=r<<11,r^=r>>>8,t[3]=t[2],t[2]=t[1],e=t[1]=t[0],t[0]=(r^e^e>>>19)>>>0}}const h=[3737844653,799659867,3827263459,1552149530,4137311345];class c extends r{constructor(t=h){super(),this.buffer=new Uint32Array(5),this.seed(t)}copy(){return new c(this.buffer)}seed(t){return this.buffer.set(t),this}bytes(){return new Uint8Array(this.buffer.buffer)}int(){const t=this.buffer;let e,r=t[3];return r^=r>>>2,r^=r<<1,t[3]=t[2],t[2]=t[1],e=t[1]=t[0],r^=e,r^=e<<4,t[0]=r,r+(t[4]+=362437)>>>0}}class a extends r{constructor(t=3737844653){super(),this.buffer=new Uint32Array(4),this.seed(t)}bytes(){return new Uint8Array(this.buffer.buffer)}copy(){const t=new a;return t.buffer.set(this.buffer),t}seed(t){const e=this.buffer;e.set([t,0,0,0]);for(let t=0,r=1;r<8;t=r++){let n=(e[3&t]^e[3&t]>>>30)>>>0;n=35173*n+((27655*n&65535)<<16)>>>0,e[3&r]^=r+n>>>0}return this}int(){const t=this.buffer;let e=t[0];return e^=e<<15,e^=e>>>18,e^=t[3]<<11,t[0]=t[1],t[1]=t[2],t[2]=t[3],t[3]=e,e+t[2]>>>0}}t.ARandom=r,t.SYSTEM=f,t.Smush32=n,t.SystemRandom=i,t.XorShift128=o,t.XorWow=c,t.XsAdd=a,t.randomID=(t=4,e="",r="abcdefghijklmnopqrstuvwxyz",n=f)=>{for(const s=r.length;--t>=0;)e+=r[0|n.float(s)];return e},t.weightedRandom=(t,r,n=f)=>{const s=t.length;e.assert(s>0,"no choices given");const i=t.map(r?(t,e)=>[t,r[e]]:t=>[t,1]).sort((t,e)=>e[1]-t[1]),u=i.reduce((t,e)=>t+e[1],0);return e.assert(u>0,"no choices given"),()=>{const t=n.float(u);let e=u;for(let r=0;r<s;r++)if(e-=i[r][1],e<=t)return i[r][0]}},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@thi.ng/checks"),require("@thi.ng/api")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/checks","@thi.ng/api"],e):e(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.random={}),t.checks,t.thi.ng.api)}(this,(function(t,e,r){"use strict";class n{float(t=1){return this.int()*(1/4294967295)*t}norm(t=1){return 2*(this.int()*(1/4294967295)-.5)*t}minmax(t,e){return this.float()*(e-t)+t}gaussian(t=10,e=-.5,r=1){let n=0,s=t;for(;s-- >0;)n+=this.float(r);return n/t+e}}class s extends n{constructor(t=3737844653){super(),this.buffer=new Uint32Array([t,0])}copy(){const t=new s;return t.buffer.set(this.buffer),t}seed(t){return this.buffer.set([t,0]),this}int(){const t=this.buffer,e=1540483477,r=t[1]++*e>>>0,n=t[0]=(r^r>>24^t[0]*e>>>0)*e>>>0;return(n^n>>>13)>>>0}}const i=Math.random;class f extends n{int(){return 4294967295*i()>>>0}float(t=1){return i()*t}norm(t=1){return 2*(i()-.5)*t}}const o=new f,u=[3737844653,799659867,3827263459,1552149530];class h extends n{constructor(t=u){super(),this.buffer=new Uint32Array(4),this.seed(t)}copy(){return new h(this.buffer)}bytes(){return new Uint8Array(this.buffer.buffer)}seed(t){return this.buffer.set(t),this}int(){const t=this.buffer;let e,r=t[3];return r^=r<<11,r^=r>>>8,t[3]=t[2],t[2]=t[1],e=t[1]=t[0],t[0]=(r^e^e>>>19)>>>0}}const c=[3737844653,799659867,3827263459,1552149530,4137311345];class a extends n{constructor(t=c){super(),this.buffer=new Uint32Array(5),this.seed(t)}copy(){return new a(this.buffer)}seed(t){return this.buffer.set(t),this}bytes(){return new Uint8Array(this.buffer.buffer)}int(){const t=this.buffer;let e,r=t[3];return r^=r>>>2,r^=r<<1,t[3]=t[2],t[2]=t[1],e=t[1]=t[0],r^=e,r^=e<<4,t[0]=r,r+(t[4]+=362437)>>>0}}class d extends n{constructor(t=3737844653){super(),this.buffer=new Uint32Array(4),this.seed(t)}bytes(){return new Uint8Array(this.buffer.buffer)}copy(){const t=new d;return t.buffer.set(this.buffer),t}seed(t){const e=this.buffer;e.set([t,0,0,0]);for(let t=0,r=1;r<8;t=r++){let n=(e[3&t]^e[3&t]>>>30)>>>0;n=35173*n+((27655*n&65535)<<16)>>>0,e[3&r]^=r+n>>>0}return this}int(){const t=this.buffer;let e=t[0];return e^=e<<15,e^=e>>>18,e^=t[3]<<11,t[0]=t[1],t[1]=t[2],t[2]=t[3],t[3]=e,e+t[2]>>>0}}const b=e.hasCrypto()?t=>window.crypto.getRandomValues(t):t=>{const e=t.length;for(let r=0;r<e;r++)t[r]=255&o.int();return t};t.ARandom=n,t.SYSTEM=o,t.Smush32=s,t.SystemRandom=f,t.XorShift128=h,t.XorWow=a,t.XsAdd=d,t.randomBytes=b,t.randomID=(t=4,e="",r="abcdefghijklmnopqrstuvwxyz",n=o)=>{for(const s=r.length;--t>=0;)e+=r[0|n.float(s)];return e},t.weightedRandom=(t,e,n=o)=>{const s=t.length;r.assert(s>0,"no choices given");const i=t.map(e?(t,r)=>[t,e[r]]:t=>[t,1]).sort((t,e)=>e[1]-t[1]),f=i.reduce((t,e)=>t+e[1],0);return r.assert(f>0,"no choices given"),()=>{const t=n.float(f);let e=f;for(let r=0;r<s;r++)if(e-=i[r][1],e<=t)return i[r][0]}},Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/random", | ||
"version": "1.1.15", | ||
"version": "1.2.0", | ||
"description": "Pseudo-random number generators w/ unified API", | ||
@@ -41,3 +41,3 @@ "module": "./index.js", | ||
"dependencies": { | ||
"@thi.ng/api": "^6.7.0" | ||
"@thi.ng/api": "^6.7.1" | ||
}, | ||
@@ -55,3 +55,3 @@ "keywords": [ | ||
"sideEffects": false, | ||
"gitHead": "93d8af817724c1c5b06d80ffa2492fe5b4fb7bc4" | ||
"gitHead": "38d193103121815a5794ae242817f8f306b6ddd4" | ||
} |
@@ -18,2 +18,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
- [API](#api) | ||
- [Other utilities](#other-utilities) | ||
- [Authors](#authors) | ||
@@ -49,3 +50,3 @@ - [License](#license) | ||
Package sizes (gzipped): ESM: 1.0KB / CJS: 1.1KB / UMD: 1.1KB | ||
Package sizes (gzipped): ESM: 1.1KB / CJS: 1.2KB / UMD: 1.2KB | ||
@@ -112,2 +113,7 @@ ## Dependencies | ||
### Other utilities | ||
- [randomBytes](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-bytes.ts) | ||
- [randomID](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-id.ts) | ||
## Authors | ||
@@ -114,0 +120,0 @@ |
@@ -85,2 +85,7 @@ # ${pkg.name} | ||
### Other utilities | ||
- [randomBytes](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-bytes.ts) | ||
- [randomID](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-id.ts) | ||
## Authors | ||
@@ -87,0 +92,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59744
29
599
123
Updated@thi.ng/api@^6.7.1