@thi.ng/random
Advanced tools
Comparing version 1.0.2 to 1.1.0
import { ICopy } from "@thi.ng/api"; | ||
export interface IRandom { | ||
int(): number; | ||
float(): number; | ||
float(norm?: number): number; | ||
norm(scale?: number): number; | ||
@@ -6,0 +6,0 @@ minmax(min: number, max: number): number; |
@@ -6,2 +6,18 @@ # Change Log | ||
# [1.1.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@1.0.2...@thi.ng/random@1.1.0) (2019-02-15) | ||
### Bug Fixes | ||
* **random:** add opt scale arg to IRandom.float() ([5a7e448](https://github.com/thi-ng/umbrella/commit/5a7e448)) | ||
### Features | ||
* **random:** add randomID() & weightedRandom() ([f719724](https://github.com/thi-ng/umbrella/commit/f719724)) | ||
## [1.0.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/random@1.0.1...@thi.ng/random@1.0.2) (2019-02-05) | ||
@@ -8,0 +24,0 @@ |
@@ -7,1 +7,3 @@ export * from "./api"; | ||
export * from "./xsadd"; | ||
export * from "./random-id"; | ||
export * from "./weighted-random"; |
@@ -7,1 +7,3 @@ export * from "./api"; | ||
export * from "./xsadd"; | ||
export * from "./random-id"; | ||
export * from "./weighted-random"; |
@@ -162,2 +162,30 @@ 'use strict'; | ||
const randomID = (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz", rnd = SYSTEM) => { | ||
for (const n = syms.length; --len >= 0;) { | ||
prefix += syms[rnd.float(n) | 0]; | ||
} | ||
return prefix; | ||
}; | ||
const weightedRandom = (choices, weights, rnd = SYSTEM) => { | ||
const opts = choices.map(weights ? | ||
(x, i) => [x, weights[i]] : | ||
(x) => [x, 1]).sort((a, b) => b[1] - a[1]); | ||
const n = choices.length; | ||
let total = 0, i, r, sum; | ||
for (i = 0; i < n; i++) { | ||
total += opts[i][1]; | ||
} | ||
return () => { | ||
r = rnd.float(total); | ||
sum = total; | ||
for (i = 0; i < n; i++) { | ||
sum -= opts[i][1]; | ||
if (sum <= r) { | ||
return opts[i][0]; | ||
} | ||
} | ||
}; | ||
}; | ||
exports.ARandom = ARandom; | ||
@@ -170,1 +198,3 @@ exports.Smush32 = Smush32; | ||
exports.XsAdd = XsAdd; | ||
exports.randomID = randomID; | ||
exports.weightedRandom = weightedRandom; |
@@ -164,2 +164,30 @@ (function (global, factory) { | ||
const randomID = (len = 4, prefix = "", syms = "abcdefghijklmnopqrstuvwxyz", rnd = SYSTEM) => { | ||
for (const n = syms.length; --len >= 0;) { | ||
prefix += syms[rnd.float(n) | 0]; | ||
} | ||
return prefix; | ||
}; | ||
const weightedRandom = (choices, weights, rnd = SYSTEM) => { | ||
const opts = choices.map(weights ? | ||
(x, i) => [x, weights[i]] : | ||
(x) => [x, 1]).sort((a, b) => b[1] - a[1]); | ||
const n = choices.length; | ||
let total = 0, i, r, sum; | ||
for (i = 0; i < n; i++) { | ||
total += opts[i][1]; | ||
} | ||
return () => { | ||
r = rnd.float(total); | ||
sum = total; | ||
for (i = 0; i < n; i++) { | ||
sum -= opts[i][1]; | ||
if (sum <= r) { | ||
return opts[i][0]; | ||
} | ||
} | ||
}; | ||
}; | ||
exports.ARandom = ARandom; | ||
@@ -172,2 +200,4 @@ exports.Smush32 = Smush32; | ||
exports.XsAdd = XsAdd; | ||
exports.randomID = randomID; | ||
exports.weightedRandom = weightedRandom; | ||
@@ -174,0 +204,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "@thi.ng/random", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Pseudo-random number generators w/ unified API", | ||
@@ -48,3 +48,3 @@ "module": "./index.js", | ||
"sideEffects": false, | ||
"gitHead": "74fb4d83013785ce7a95357c565426a046657e74" | ||
"gitHead": "418f7af20c8ce3098e86bbc3a2377ccbac761b70" | ||
} |
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
52135
26
730