@aryth/rand
Advanced tools
Comparing version 0.3.16 to 0.3.17
@@ -9,5 +9,5 @@ 'use strict'; | ||
const { | ||
random | ||
random: random$1 | ||
} = Math; | ||
const rand = l => ~~(random() * l); | ||
const rand = l => ~~(random$1() * l); | ||
/** | ||
@@ -34,5 +34,5 @@ * From [min, max) return a random integer. | ||
while (digit > 20) digit -= 20, t += random().toFixed(20).substring(2); | ||
while (digit > 20) digit -= 20, t += random$1().toFixed(20).substring(2); | ||
return t + random().toFixed(digit).substring(2); | ||
return t + random$1().toFixed(digit).substring(2); | ||
}; | ||
@@ -67,5 +67,5 @@ /** | ||
while (digit > 20) digit -= 20, t += random().toFixed(20).substring(2); | ||
while (digit > 20) digit -= 20, t += random$1().toFixed(20).substring(2); | ||
return t + random().toFixed(digit).substring(2); | ||
return t + random$1().toFixed(digit).substring(2); | ||
}; | ||
@@ -120,2 +120,119 @@ | ||
const { | ||
random, | ||
abs, | ||
exp, | ||
log, | ||
sqrt, | ||
pow, | ||
cos, | ||
sin, | ||
PI | ||
} = Math; | ||
const R0 = 3.442619855899; | ||
const R1 = 1.0 / R0; | ||
const R0S = exp(-0.5 * R0 * R0); | ||
const N2P32 = -pow(2, 32); | ||
const M1 = 2147483648.0; | ||
const VN = 9.91256303526217e-3; | ||
class Ziggurat { | ||
constructor(mean = 0, stdev = 1) { | ||
this.jsr = 123456789; | ||
this.wn = Array(128); | ||
this.fn = Array(128); | ||
this.kn = Array(128); | ||
this.mean = mean; | ||
this.stdev = stdev; | ||
this.preset(); | ||
} | ||
static build(mean, stdev) { | ||
return new Ziggurat(mean, stdev); | ||
} | ||
preset() { | ||
this.jsr ^= new Date().getTime(); // seed generator based on current time | ||
let m1 = M1, | ||
dn = R0, | ||
tn = R0, | ||
vn = VN, | ||
q = vn / R0S; | ||
this.kn[0] = ~~(dn / q * m1); | ||
this.kn[1] = 0; | ||
this.wn[0] = q / m1; | ||
this.wn[127] = dn / m1; | ||
this.fn[0] = 1.0; | ||
this.fn[127] = R0S; | ||
for (let i = 126; i >= 1; i--) { | ||
dn = sqrt(-2.0 * log(vn / dn + exp(-0.5 * dn * dn))); | ||
this.kn[i + 1] = ~~(dn / tn * m1); | ||
tn = dn; | ||
this.fn[i] = exp(-0.5 * dn * dn); | ||
this.wn[i] = dn / m1; | ||
} | ||
} | ||
next() { | ||
return this.randSample() * this.stdev + this.mean; | ||
} | ||
nextInt() { | ||
return Math.round(this.next()); | ||
} // * generator() { while (true) yield this.next() } | ||
randSample() { | ||
let hz = this.xorshift(), | ||
iz = hz & 127; | ||
return abs(hz) < this.kn[iz] ? hz * this.wn[iz] : this.nfix(hz, iz); | ||
} | ||
nfix(hz, iz) { | ||
let r = R0, | ||
x, | ||
y; | ||
while (true) { | ||
x = hz * this.wn[iz]; | ||
if (iz === 0) { | ||
do { | ||
x = -log(this.uni()) * R1; | ||
y = -log(this.uni()); | ||
} while (y + y < x * x); // { | ||
// x = -log(this.uni()) * r1 | ||
// y = -log(this.uni()) | ||
// } | ||
return hz > 0 ? r + x : -r - x; | ||
} | ||
if (this.fn[iz] + this.uni() * (this.fn[iz - 1] - this.fn[iz]) < exp(-0.5 * x * x)) return x; | ||
hz = this.xorshift(); | ||
iz = hz & 127; | ||
if (abs(hz) < this.kn[iz]) return hz * this.wn[iz]; | ||
} | ||
} | ||
xorshift() { | ||
let m = this.jsr, | ||
n = m; | ||
n ^= n << 13; | ||
n ^= n >>> 17; | ||
n ^= n << 5; | ||
this.jsr = n; | ||
return m + n | 0; | ||
} | ||
uni() { | ||
return 0.5 + this.xorshift() / N2P32; | ||
} | ||
} | ||
exports.Ziggurat = Ziggurat; | ||
exports.flop = flop; | ||
@@ -122,0 +239,0 @@ exports.flopEntry = flopEntry; |
@@ -5,5 +5,5 @@ import { swap } from '@vect/swap'; | ||
const { | ||
random | ||
random: random$1 | ||
} = Math; | ||
const rand = l => ~~(random() * l); | ||
const rand = l => ~~(random$1() * l); | ||
/** | ||
@@ -30,5 +30,5 @@ * From [min, max) return a random integer. | ||
while (digit > 20) digit -= 20, t += random().toFixed(20).substring(2); | ||
while (digit > 20) digit -= 20, t += random$1().toFixed(20).substring(2); | ||
return t + random().toFixed(digit).substring(2); | ||
return t + random$1().toFixed(digit).substring(2); | ||
}; | ||
@@ -63,5 +63,5 @@ /** | ||
while (digit > 20) digit -= 20, t += random().toFixed(20).substring(2); | ||
while (digit > 20) digit -= 20, t += random$1().toFixed(20).substring(2); | ||
return t + random().toFixed(digit).substring(2); | ||
return t + random$1().toFixed(digit).substring(2); | ||
}; | ||
@@ -116,2 +116,118 @@ | ||
export { flop, flopEntry, flopGenerator, flopIndex, flopKey, flopValue, rand, randBetw, randIn, randInt, randIntBetw, randLong, randLongStr, shuffle }; | ||
const { | ||
random, | ||
abs, | ||
exp, | ||
log, | ||
sqrt, | ||
pow, | ||
cos, | ||
sin, | ||
PI | ||
} = Math; | ||
const R0 = 3.442619855899; | ||
const R1 = 1.0 / R0; | ||
const R0S = exp(-0.5 * R0 * R0); | ||
const N2P32 = -pow(2, 32); | ||
const M1 = 2147483648.0; | ||
const VN = 9.91256303526217e-3; | ||
class Ziggurat { | ||
constructor(mean = 0, stdev = 1) { | ||
this.jsr = 123456789; | ||
this.wn = Array(128); | ||
this.fn = Array(128); | ||
this.kn = Array(128); | ||
this.mean = mean; | ||
this.stdev = stdev; | ||
this.preset(); | ||
} | ||
static build(mean, stdev) { | ||
return new Ziggurat(mean, stdev); | ||
} | ||
preset() { | ||
this.jsr ^= new Date().getTime(); // seed generator based on current time | ||
let m1 = M1, | ||
dn = R0, | ||
tn = R0, | ||
vn = VN, | ||
q = vn / R0S; | ||
this.kn[0] = ~~(dn / q * m1); | ||
this.kn[1] = 0; | ||
this.wn[0] = q / m1; | ||
this.wn[127] = dn / m1; | ||
this.fn[0] = 1.0; | ||
this.fn[127] = R0S; | ||
for (let i = 126; i >= 1; i--) { | ||
dn = sqrt(-2.0 * log(vn / dn + exp(-0.5 * dn * dn))); | ||
this.kn[i + 1] = ~~(dn / tn * m1); | ||
tn = dn; | ||
this.fn[i] = exp(-0.5 * dn * dn); | ||
this.wn[i] = dn / m1; | ||
} | ||
} | ||
next() { | ||
return this.randSample() * this.stdev + this.mean; | ||
} | ||
nextInt() { | ||
return Math.round(this.next()); | ||
} // * generator() { while (true) yield this.next() } | ||
randSample() { | ||
let hz = this.xorshift(), | ||
iz = hz & 127; | ||
return abs(hz) < this.kn[iz] ? hz * this.wn[iz] : this.nfix(hz, iz); | ||
} | ||
nfix(hz, iz) { | ||
let r = R0, | ||
x, | ||
y; | ||
while (true) { | ||
x = hz * this.wn[iz]; | ||
if (iz === 0) { | ||
do { | ||
x = -log(this.uni()) * R1; | ||
y = -log(this.uni()); | ||
} while (y + y < x * x); // { | ||
// x = -log(this.uni()) * r1 | ||
// y = -log(this.uni()) | ||
// } | ||
return hz > 0 ? r + x : -r - x; | ||
} | ||
if (this.fn[iz] + this.uni() * (this.fn[iz - 1] - this.fn[iz]) < exp(-0.5 * x * x)) return x; | ||
hz = this.xorshift(); | ||
iz = hz & 127; | ||
if (abs(hz) < this.kn[iz]) return hz * this.wn[iz]; | ||
} | ||
} | ||
xorshift() { | ||
let m = this.jsr, | ||
n = m; | ||
n ^= n << 13; | ||
n ^= n >>> 17; | ||
n ^= n << 5; | ||
this.jsr = n; | ||
return m + n | 0; | ||
} | ||
uni() { | ||
return 0.5 + this.xorshift() / N2P32; | ||
} | ||
} | ||
export { Ziggurat, flop, flopEntry, flopGenerator, flopIndex, flopKey, flopValue, rand, randBetw, randIn, randInt, randIntBetw, randLong, randLongStr, shuffle }; |
{ | ||
"name": "@aryth/rand", | ||
"version": "0.3.16", | ||
"version": "0.3.17", | ||
"description": "A math util library", | ||
@@ -18,3 +18,3 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@aryth/comparer": "^0.3.16", | ||
"@aryth/comparer": "^0.3.17", | ||
"@vect/swap": "^0.3.9" | ||
@@ -37,3 +37,3 @@ }, | ||
"homepage": "https://github.com/hoyeungw/aryth#readme", | ||
"gitHead": "ea474d532ec8e4b99abb366e488585a69fce9794" | ||
"gitHead": "18a29f8be001a550c00759e92a823699483c106c" | ||
} |
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
14255
388
Updated@aryth/comparer@^0.3.17