@thi.ng/random
Advanced tools
Comparing version 1.4.17 to 2.0.0
28
api.d.ts
import type { ICopy } from "@thi.ng/api"; | ||
export interface IRandom { | ||
export interface INorm { | ||
/** | ||
* Returns float in [-scale..scale) interval. | ||
* | ||
* @param scale - default 1 | ||
*/ | ||
norm(scale?: number): number; | ||
} | ||
export interface IRandom extends INorm { | ||
/** | ||
* Returns unsigned 32bit int | ||
*/ | ||
int(): number; | ||
float(norm?: number): number; | ||
norm(scale?: number): number; | ||
/** | ||
* Returns float in [0..max) interval. | ||
* | ||
* @param max - default 1 | ||
*/ | ||
float(max?: number): number; | ||
/** | ||
* Returns float in [min..max) interval. | ||
* | ||
* @param min - | ||
* @param max - | ||
*/ | ||
minmax(min: number, max: number): number; | ||
gaussian(samples?: number, offset?: number, scale?: number): number; | ||
} | ||
@@ -9,0 +29,0 @@ export interface ISeedable<T> { |
@@ -7,13 +7,3 @@ import type { IRandom } from "./api"; | ||
minmax(min: number, max: number): number; | ||
/** | ||
* Returns approx. normal distribution using CLT. | ||
* | ||
* {@link https://en.wikipedia.org/wiki/Central_limit_theorem} | ||
* | ||
* @param n - | ||
* @param offset - | ||
* @param scale - | ||
*/ | ||
gaussian(n?: number, offset?: number, scale?: number): number; | ||
} | ||
//# sourceMappingURL=arandom.d.ts.map |
@@ -12,18 +12,2 @@ const INV_MAX = 1 / 0xffffffff; | ||
} | ||
/** | ||
* Returns approx. normal distribution using CLT. | ||
* | ||
* {@link https://en.wikipedia.org/wiki/Central_limit_theorem} | ||
* | ||
* @param n - | ||
* @param offset - | ||
* @param scale - | ||
*/ | ||
gaussian(n = 10, offset = -0.5, scale = 1) { | ||
let sum = 0; | ||
let m = n; | ||
while (m-- > 0) | ||
sum += this.float(scale); | ||
return sum / n + offset; | ||
} | ||
} |
@@ -6,2 +6,26 @@ # Change Log | ||
# [2.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@1.4.17...@thi.ng/random@2.0.0) (2020-08-28) | ||
### Bug Fixes | ||
* **random:** off-by-one error in SYSTEM.int() ([ca0492d](https://github.com/thi-ng/umbrella/commit/ca0492d2f5f867c8945c279f60cf908037df1385)) | ||
### Features | ||
* **random:** add INorm, extract gaussianCLT() ([c687598](https://github.com/thi-ng/umbrella/commit/c687598f87283a77c109d6b378b1907349eab760)) | ||
### BREAKING CHANGES | ||
* **random:** remove gaussian() from IRandom, | ||
extract as standalone gaussianCLT() | ||
- update gaussianCLT() default args to be more meaningful | ||
## [1.4.17](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@1.4.16...@thi.ng/random@1.4.17) (2020-08-17) | ||
@@ -8,0 +32,0 @@ |
@@ -9,2 +9,3 @@ export * from "./api"; | ||
export * from "./xsadd"; | ||
export * from "./gaussian-clt"; | ||
export * from "./random-bytes"; | ||
@@ -11,0 +12,0 @@ export * from "./random-id"; |
@@ -9,2 +9,3 @@ export * from "./api"; | ||
export * from "./xsadd"; | ||
export * from "./gaussian-clt"; | ||
export * from "./random-bytes"; | ||
@@ -11,0 +12,0 @@ export * from "./random-id"; |
@@ -19,9 +19,2 @@ 'use strict'; | ||
} | ||
gaussian(n = 10, offset = -0.5, scale = 1) { | ||
let sum = 0; | ||
let m = n; | ||
while (m-- > 0) | ||
sum += this.float(scale); | ||
return sum / n + offset; | ||
} | ||
} | ||
@@ -64,3 +57,3 @@ | ||
int() { | ||
return (random() * 0xffffffff) >>> 0; | ||
return (random() * 4294967296) >>> 0; | ||
} | ||
@@ -207,2 +200,10 @@ float(norm = 1) { | ||
const gaussian = (rnd = SYSTEM, n = 24, offset = 0, scale = 1) => () => { | ||
let sum = 0; | ||
let m = n; | ||
while (m-- > 0) | ||
sum += rnd.norm(scale); | ||
return sum / n + offset; | ||
}; | ||
const randomBytes = checks.hasCrypto() | ||
@@ -219,3 +220,4 @@ ? (buf) => window.crypto.getRandomValues(buf) | ||
const randomID = (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz", rnd = SYSTEM) => { | ||
for (const n = syms.length; --len >= 0;) { | ||
const n = syms.length; | ||
for (; --len >= 0;) { | ||
prefix += syms[rnd.float(n) | 0]; | ||
@@ -264,2 +266,3 @@ } | ||
exports.XsAdd = XsAdd; | ||
exports.gaussian = gaussian; | ||
exports.randomBytes = randomBytes; | ||
@@ -266,0 +269,0 @@ exports.randomID = randomID; |
@@ -1,1 +0,1 @@ | ||
!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="undefined"!=typeof globalThis?globalThis:t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.random={}),t.thi.ng.checks,t.thi.ng.api)}(this,(function(t,e,r){"use strict";class n{float(t=1){return 2.3283064370807974e-10*this.int()*t}norm(t=1){return 2*(2.3283064370807974e-10*this.int()-.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}}const s=[3737844653,799659867,3827263459,1552149530],i=[...s,4137311345];class f extends n{constructor(t=3737844653){super(),this.buffer=new Uint32Array([t,0])}copy(){const t=new f;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 u=Math.random;class o extends n{int(){return 4294967295*u()>>>0}float(t=1){return u()*t}norm(t=1){return 2*(u()-.5)*t}}const h=new o;class c extends n{constructor(t=s){super(),this.buffer=new Uint32Array(4),this.seed(t)}copy(){return new c(this.buffer)}bytes(){return new Uint8Array(this.buffer.buffer)}seed(t){return this.buffer.set(t),this}int(){const t=this.buffer;let e=t[0]+t[3];const r=(e<<7|e>>>25)>>>0;return e=t[1]<<9,t[2]^=t[0],t[3]^=t[1],t[1]^=t[2],t[0]^=t[3],t[2]^=e,e=t[3],t[3]=(e<<11|e>>>21)>>>0,r}}class a extends n{constructor(t=s){super(),this.buffer=new Uint32Array(4),this.seed(t)}copy(){return new a(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}}class d extends n{constructor(t=i){super(),this.buffer=new Uint32Array(5),this.seed(t)}copy(){return new d(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 b 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 b;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 l=e.hasCrypto()?t=>window.crypto.getRandomValues(t):t=>{const e=t.length;for(let r=0;r<e;r++)t[r]=255&h.int();return t};t.ARandom=n,t.SYSTEM=h,t.Smush32=f,t.SystemRandom=o,t.XorShift128=a,t.XorWow=d,t.Xoshiro128=c,t.XsAdd=b,t.randomBytes=l,t.randomID=(t=4,e="",r="abcdefghijklmnopqrstuvwxyz",n=h)=>{for(const s=r.length;--t>=0;)e+=r[0|n.float(s)];return e},t.uuidv4Bytes=t=>((t=l(t||new Uint8Array(16)))[6]=64|15&t[6],t[8]=128|63&t[8],t),t.weightedRandom=(t,e,n=h)=>{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})})); | ||
!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="undefined"!=typeof globalThis?globalThis:t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.random={}),t.thi.ng.checks,t.thi.ng.api)}(this,(function(t,e,r){"use strict";const n=1/4294967295;class s{float(t=1){return this.int()*n*t}norm(t=1){return 2*(this.int()*n-.5)*t}minmax(t,e){return this.float()*(e-t)+t}}const i=[3737844653,799659867,3827263459,1552149530],u=[...i,4137311345];class f extends s{constructor(t=3737844653){super(),this.buffer=new Uint32Array([t,0])}copy(){const t=new f;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 o=Math.random;class h extends s{int(){return 4294967296*o()>>>0}float(t=1){return o()*t}norm(t=1){return 2*(o()-.5)*t}}const c=new h;class a extends s{constructor(t=i){super(),this.buffer=new Uint32Array(4),this.seed(t)}copy(){return new a(this.buffer)}bytes(){return new Uint8Array(this.buffer.buffer)}seed(t){return this.buffer.set(t),this}int(){const t=this.buffer;let e=t[0]+t[3];const r=(e<<7|e>>>25)>>>0;return e=t[1]<<9,t[2]^=t[0],t[3]^=t[1],t[1]^=t[2],t[0]^=t[3],t[2]^=e,e=t[3],t[3]=(e<<11|e>>>21)>>>0,r}}class d extends s{constructor(t=i){super(),this.buffer=new Uint32Array(4),this.seed(t)}copy(){return new d(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}}class b extends s{constructor(t=u){super(),this.buffer=new Uint32Array(5),this.seed(t)}copy(){return new b(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 l extends s{constructor(t=3737844653){super(),this.buffer=new Uint32Array(4),this.seed(t)}bytes(){return new Uint8Array(this.buffer.buffer)}copy(){const t=new l;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 y=e.hasCrypto()?t=>window.crypto.getRandomValues(t):t=>{const e=t.length;for(let r=0;r<e;r++)t[r]=255&c.int();return t};t.ARandom=s,t.SYSTEM=c,t.Smush32=f,t.SystemRandom=h,t.XorShift128=d,t.XorWow=b,t.Xoshiro128=a,t.XsAdd=l,t.gaussian=(t=c,e=24,r=0,n=1)=>()=>{let s=0,i=e;for(;i-- >0;)s+=t.norm(n);return s/e+r},t.randomBytes=y,t.randomID=(t=4,e="",r="abcdefghijklmnopqrstuvwxyz",n=c)=>{const s=r.length;for(;--t>=0;)e+=r[0|n.float(s)];return e},t.uuidv4Bytes=t=>((t=y(t||new Uint8Array(16)))[6]=64|15&t[6],t[8]=128|63&t[8],t),t.weightedRandom=(t,e,n=c)=>{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]),u=i.reduce((t,e)=>t+e[1],0);return r.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})})); |
{ | ||
"name": "@thi.ng/random", | ||
"version": "1.4.17", | ||
"version": "2.0.0", | ||
"description": "Pseudo-random number generators w/ unified API", | ||
@@ -42,14 +42,14 @@ "module": "./index.js", | ||
"@istanbuljs/nyc-config-typescript": "^1.0.1", | ||
"@microsoft/api-extractor": "^7.9.2", | ||
"@types/mocha": "^8.0.0", | ||
"@types/node": "^14.0.26", | ||
"mocha": "^8.1.1", | ||
"@microsoft/api-extractor": "^7.9.11", | ||
"@types/mocha": "^8.0.3", | ||
"@types/node": "^14.6.1", | ||
"mocha": "^8.1.2", | ||
"nyc": "^15.1.0", | ||
"ts-node": "^8.10.1", | ||
"ts-node": "^9.0.0", | ||
"typedoc": "^0.18.0", | ||
"typescript": "^3.9.7" | ||
"typescript": "^4.0.2" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/api": "^6.12.2", | ||
"@thi.ng/checks": "^2.7.6" | ||
"@thi.ng/api": "^6.12.3", | ||
"@thi.ng/checks": "^2.7.7" | ||
}, | ||
@@ -72,3 +72,3 @@ "files": [ | ||
"sideEffects": false, | ||
"gitHead": "44824f17050b412d635b576bf5f9b2136dac9554" | ||
"gitHead": "3a55c0df86c5be9c56a1e1a4c0cfcaa2809a160a" | ||
} |
@@ -22,3 +22,4 @@ import { SYSTEM } from "./system"; | ||
export const randomID = (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz", rnd = SYSTEM) => { | ||
for (const n = syms.length; --len >= 0;) { | ||
const n = syms.length; | ||
for (; --len >= 0;) { | ||
prefix += syms[rnd.float(n) | 0]; | ||
@@ -25,0 +26,0 @@ } |
@@ -58,3 +58,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (gzipped, pre-treeshake): ESM: 1.23 KB / CJS: 1.33 KB / UMD: 1.37 KB | ||
Package sizes (gzipped, pre-treeshake): ESM: 1.24 KB / CJS: 1.34 KB / UMD: 1.36 KB | ||
@@ -61,0 +61,0 @@ ## Dependencies |
@@ -1,4 +0,4 @@ | ||
import { ARandom } from "./arandom"; | ||
import type { IBuffered, ICopy } from "@thi.ng/api"; | ||
import type { ISeedable } from "./api"; | ||
import { ARandom } from "./arandom"; | ||
export declare class Smush32 extends ARandom implements IBuffered<Uint32Array>, ICopy<Smush32>, ISeedable<number> { | ||
@@ -5,0 +5,0 @@ buffer: Uint32Array; |
@@ -5,3 +5,3 @@ import { ARandom } from "./arandom"; | ||
int() { | ||
return (random() * 0xffffffff) >>> 0; | ||
return (random() * 4294967296) /* 2**32 */ >>> 0; | ||
} | ||
@@ -8,0 +8,0 @@ float(norm = 1) { |
@@ -0,2 +1,8 @@ | ||
/** | ||
* Uses {@link randomBytes} to fill given (optional) byte array with a UUIDv4. | ||
* Creates new Uint8Array if none given. | ||
* | ||
* @param buf - | ||
*/ | ||
export declare const uuidv4Bytes: (buf?: Uint8Array | undefined) => Uint8Array; | ||
//# sourceMappingURL=uuid.d.ts.map |
import { randomBytes } from "./random-bytes"; | ||
/** | ||
* Uses {@link randomBytes} to fill given (optional) byte array with a UUIDv4. | ||
* Creates new Uint8Array if none given. | ||
* | ||
* @param buf - | ||
*/ | ||
export const uuidv4Bytes = (buf) => { | ||
@@ -3,0 +9,0 @@ buf = randomBytes(buf || new Uint8Array(16)); |
@@ -1,4 +0,4 @@ | ||
import { ARandom } from "./arandom"; | ||
import type { IBuffered, ICopy } from "@thi.ng/api"; | ||
import type { ISeedable } from "./api"; | ||
import { ARandom } from "./arandom"; | ||
export declare class XorShift128 extends ARandom implements IBuffered<Uint32Array>, ICopy<XorShift128>, ISeedable<ArrayLike<number>> { | ||
@@ -5,0 +5,0 @@ buffer: Uint32Array; |
@@ -1,4 +0,4 @@ | ||
import { ARandom } from "./arandom"; | ||
import type { IBuffered, ICopy } from "@thi.ng/api"; | ||
import type { ISeedable } from "./api"; | ||
import { ARandom } from "./arandom"; | ||
export declare class XorWow extends ARandom implements IBuffered<Uint32Array>, ICopy<XorWow>, ISeedable<ArrayLike<number>> { | ||
@@ -5,0 +5,0 @@ buffer: Uint32Array; |
@@ -1,4 +0,4 @@ | ||
import { ARandom } from "./arandom"; | ||
import type { IBuffered, ICopy } from "@thi.ng/api"; | ||
import type { ISeedable } from "./api"; | ||
import { ARandom } from "./arandom"; | ||
export declare class Xoshiro128 extends ARandom implements IBuffered<Uint32Array>, ICopy<Xoshiro128>, ISeedable<ArrayLike<number>> { | ||
@@ -5,0 +5,0 @@ buffer: Uint32Array; |
@@ -1,4 +0,4 @@ | ||
import { ARandom } from "./arandom"; | ||
import type { IBuffered, ICopy } from "@thi.ng/api"; | ||
import type { ISeedable } from "./api"; | ||
import { ARandom } from "./arandom"; | ||
export declare class XsAdd extends ARandom implements IBuffered<Uint32Array>, ICopy<XsAdd>, ISeedable<number> { | ||
@@ -5,0 +5,0 @@ buffer: Uint32Array; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
67370
38
760
Updated@thi.ng/api@^6.12.3
Updated@thi.ng/checks@^2.7.7