@aryth/rand
Advanced tools
Comparing version 0.3.17 to 0.3.18
@@ -7,2 +7,3 @@ 'use strict'; | ||
var comparer = require('@aryth/comparer'); | ||
var nullish = require('@typen/nullish'); | ||
@@ -138,9 +139,3 @@ const { | ||
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(); | ||
this.bootstrap(mean, stdev); | ||
} | ||
@@ -152,3 +147,9 @@ | ||
preset() { | ||
bootstrap(mean, stdev) { | ||
this.mean = mean; | ||
this.stdev = stdev; | ||
this.jsr = 123456789; | ||
this.wn = Array(128); | ||
this.fn = Array(128); | ||
this.kn = Array(128); | ||
this.jsr ^= new Date().getTime(); // seed generator based on current time | ||
@@ -236,2 +237,48 @@ | ||
/** | ||
* | ||
* applicable for smaller number | ||
* @param {number} x | ||
* @returns {number} | ||
*/ | ||
const round = x => x + (x > 0 ? 0.5 : -0.5) << 0; | ||
const E2 = 1E+2; | ||
const roundD1 = x => Math.round(x * 10) / 10; | ||
const roundD2 = x => Math.round(x * E2) / E2; | ||
/** | ||
* | ||
* @param {number} mean | ||
* @param {number} stdev | ||
* @param {number} [digits] | ||
* @yields {number} next random norm dist. number simulated by the ziggurat algorithm. | ||
*/ | ||
function* zigguratGenerator(mean, stdev, digits) { | ||
this.bootstrap(mean, stdev); | ||
if (nullish.nullish(digits)) { | ||
while (true) yield this.randSample() * this.stdev + this.mean; | ||
} else { | ||
if (digits > 2) this.D = 10 ^ digits; | ||
const rounder = digits === 0 ? round : digits === 1 ? roundD1 : digits === 2 ? roundD2 : x => Math.round(x * this.D) / this.D; | ||
while (true) yield rounder(this.randSample() * this.stdev + this.mean); | ||
} | ||
} | ||
/** | ||
* | ||
* @param {number} mean | ||
* @param {number} stdev | ||
* @param {number} [digits] | ||
* @returns {Generator<*, void, *>} | ||
*/ | ||
const ziggurat = (mean, stdev, digits) => zigguratGenerator.call(Ziggurat.prototype, mean, stdev, digits); | ||
exports.Ziggurat = Ziggurat; | ||
@@ -252,1 +299,2 @@ exports.flop = flop; | ||
exports.shuffle = shuffle; | ||
exports.ziggurat = ziggurat; |
import { swap } from '@vect/swap'; | ||
import { max } from '@aryth/comparer'; | ||
import { nullish } from '@typen/nullish'; | ||
@@ -133,9 +134,3 @@ const { | ||
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(); | ||
this.bootstrap(mean, stdev); | ||
} | ||
@@ -147,3 +142,9 @@ | ||
preset() { | ||
bootstrap(mean, stdev) { | ||
this.mean = mean; | ||
this.stdev = stdev; | ||
this.jsr = 123456789; | ||
this.wn = Array(128); | ||
this.fn = Array(128); | ||
this.kn = Array(128); | ||
this.jsr ^= new Date().getTime(); // seed generator based on current time | ||
@@ -231,2 +232,48 @@ | ||
export { Ziggurat, flop, flopEntry, flopGenerator, flopIndex, flopKey, flopValue, rand, randBetw, randIn, randInt, randIntBetw, randLong, randLongStr, shuffle }; | ||
/** | ||
* | ||
* applicable for smaller number | ||
* @param {number} x | ||
* @returns {number} | ||
*/ | ||
const round = x => x + (x > 0 ? 0.5 : -0.5) << 0; | ||
const E2 = 1E+2; | ||
const roundD1 = x => Math.round(x * 10) / 10; | ||
const roundD2 = x => Math.round(x * E2) / E2; | ||
/** | ||
* | ||
* @param {number} mean | ||
* @param {number} stdev | ||
* @param {number} [digits] | ||
* @yields {number} next random norm dist. number simulated by the ziggurat algorithm. | ||
*/ | ||
function* zigguratGenerator(mean, stdev, digits) { | ||
this.bootstrap(mean, stdev); | ||
if (nullish(digits)) { | ||
while (true) yield this.randSample() * this.stdev + this.mean; | ||
} else { | ||
if (digits > 2) this.D = 10 ^ digits; | ||
const rounder = digits === 0 ? round : digits === 1 ? roundD1 : digits === 2 ? roundD2 : x => Math.round(x * this.D) / this.D; | ||
while (true) yield rounder(this.randSample() * this.stdev + this.mean); | ||
} | ||
} | ||
/** | ||
* | ||
* @param {number} mean | ||
* @param {number} stdev | ||
* @param {number} [digits] | ||
* @returns {Generator<*, void, *>} | ||
*/ | ||
const ziggurat = (mean, stdev, digits) => zigguratGenerator.call(Ziggurat.prototype, mean, stdev, digits); | ||
export { Ziggurat, flop, flopEntry, flopGenerator, flopIndex, flopKey, flopValue, rand, randBetw, randIn, randInt, randIntBetw, randLong, randLongStr, shuffle, ziggurat }; |
{ | ||
"name": "@aryth/rand", | ||
"version": "0.3.17", | ||
"version": "0.3.18", | ||
"description": "A math util library", | ||
@@ -18,3 +18,4 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@aryth/comparer": "^0.3.17", | ||
"@aryth/comparer": "^0.3.18", | ||
"@typen/nullish": "^0.1.16", | ||
"@vect/swap": "^0.3.9" | ||
@@ -37,3 +38,3 @@ }, | ||
"homepage": "https://github.com/hoyeungw/aryth#readme", | ||
"gitHead": "18a29f8be001a550c00759e92a823699483c106c" | ||
"gitHead": "4c78f9496801184a8ed6bf8fa3fd1b06d3b81cf8" | ||
} |
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
16678
461
3
+ Added@typen/nullish@^0.1.16
+ Added@typen/nullish@0.1.19(transitive)
Updated@aryth/comparer@^0.3.18