@thi.ng/random 3.2.6 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -1
- package/README.md +11 -2
- package/api.d.ts +7 -0
- package/arandom.d.ts +1 -0
- package/arandom.js +6 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +6 -3
- package/pick-random.d.ts +9 -1
- package/pick-random.js +7 -0
- package/sfc32.d.ts +19 -0
- package/sfc32.js +35 -0
- package/weighted-random.d.ts +10 -0
- package/weighted-random.js +12 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-05-
|
|
3
|
+
- **Last updated**: 2022-05-22T17:15:18Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,13 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [3.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.3.0) (2022-05-22)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add pickRandomKey(), weightedRandomKey() ([9b8ed07](https://github.com/thi-ng/umbrella/commit/9b8ed07))
|
|
17
|
+
- update IRandom, add SFC32 impl ([970d7f4](https://github.com/thi-ng/umbrella/commit/970d7f4))
|
|
18
|
+
|
|
12
19
|
### [3.2.2](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.2.2) (2021-11-21)
|
|
13
20
|
|
|
14
21
|
#### 🩹 Bug fixes
|
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ This project is part of the
|
|
|
13
13
|
- [Random distributions](#random-distributions)
|
|
14
14
|
- [Other utilities](#other-utilities)
|
|
15
15
|
- [Status](#status)
|
|
16
|
+
- [Support packages](#support-packages)
|
|
16
17
|
- [Related packages](#related-packages)
|
|
17
18
|
- [Installation](#installation)
|
|
18
19
|
- [Dependencies](#dependencies)
|
|
@@ -30,6 +31,7 @@ pseudo-random number generator implementations, incl. `IRandom` wrappers for
|
|
|
30
31
|
`Math.random()` and `window.crypto`:
|
|
31
32
|
|
|
32
33
|
- [Crypto](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/crypto.ts)
|
|
34
|
+
- [SFC32](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/sfc32.ts)
|
|
33
35
|
- [Smush32](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/smush32.ts)
|
|
34
36
|
- [System](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/system.ts)
|
|
35
37
|
- [Xoshiro128](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/xoshiro128.ts)
|
|
@@ -50,9 +52,10 @@ Partially ported from C implementations taken from [c.thi.ng](http://c.thi.ng).
|
|
|
50
52
|
### Other utilities
|
|
51
53
|
|
|
52
54
|
- [`coin()` / `fairCoin()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/coin.ts)
|
|
55
|
+
- [`pickRandom()` / `pickRandomKey()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/pick-random.ts)
|
|
53
56
|
- [`randomBytes()` / `randomBytesFrom()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-bytes.ts)
|
|
54
57
|
- [`randomID()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/random-id.ts)
|
|
55
|
-
- [`weightedRandom()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/weighted-random.ts)
|
|
58
|
+
- [`weightedRandom()` / `weightedRandomKey()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/weighted-random.ts)
|
|
56
59
|
- [`uniqueIndices()` / `uniqueValuesFrom()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/unique-indices.ts)
|
|
57
60
|
- [`uuidv4Bytes()` / `uuid()`](https://github.com/thi-ng/umbrella/tree/develop/packages/random/src/uuid.ts)
|
|
58
61
|
|
|
@@ -62,6 +65,10 @@ Partially ported from C implementations taken from [c.thi.ng](http://c.thi.ng).
|
|
|
62
65
|
|
|
63
66
|
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Brandom%5D+in%3Atitle)
|
|
64
67
|
|
|
68
|
+
### Support packages
|
|
69
|
+
|
|
70
|
+
- [@thi.ng/random-fxhash](https://github.com/thi-ng/umbrella/tree/develop/packages/random-fxhash) - [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random) compatible wrapper & utilities for fxhash's PRNG
|
|
71
|
+
|
|
65
72
|
### Related packages
|
|
66
73
|
|
|
67
74
|
- [@thi.ng/ksuid](https://github.com/thi-ng/umbrella/tree/develop/packages/ksuid) - Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions
|
|
@@ -89,7 +96,7 @@ node --experimental-repl-await
|
|
|
89
96
|
> const random = await import("@thi.ng/random");
|
|
90
97
|
```
|
|
91
98
|
|
|
92
|
-
Package sizes (gzipped, pre-treeshake): ESM: 1.
|
|
99
|
+
Package sizes (gzipped, pre-treeshake): ESM: 1.99 KB
|
|
93
100
|
|
|
94
101
|
## Dependencies
|
|
95
102
|
|
|
@@ -141,6 +148,8 @@ rnd.norm(100)
|
|
|
141
148
|
// next float in given interval [min .. max)
|
|
142
149
|
rnd.minmax(10, 20)
|
|
143
150
|
// 15.295951807707537
|
|
151
|
+
|
|
152
|
+
rnd.minmaxInt(10, 20)
|
|
144
153
|
```
|
|
145
154
|
|
|
146
155
|
## Authors
|
package/api.d.ts
CHANGED
|
@@ -29,6 +29,13 @@ export interface IRandom extends INorm {
|
|
|
29
29
|
* @param max -
|
|
30
30
|
*/
|
|
31
31
|
minmax(min: number, max: number): number;
|
|
32
|
+
/**
|
|
33
|
+
* Returns int in [min..max) interval.
|
|
34
|
+
*
|
|
35
|
+
* @param min -
|
|
36
|
+
* @param max -
|
|
37
|
+
*/
|
|
38
|
+
minmaxInt(min: number, max: number): number;
|
|
32
39
|
}
|
|
33
40
|
export interface ISeedable<T> {
|
|
34
41
|
seed(n: T): this;
|
package/arandom.d.ts
CHANGED
package/arandom.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const INV_MAX = 1 /
|
|
1
|
+
const INV_MAX = 1 / 2 ** 32;
|
|
2
2
|
export class ARandom {
|
|
3
3
|
float(norm = 1) {
|
|
4
4
|
return this.int() * INV_MAX * norm;
|
|
@@ -9,4 +9,9 @@ export class ARandom {
|
|
|
9
9
|
minmax(min, max) {
|
|
10
10
|
return this.float() * (max - min) + min;
|
|
11
11
|
}
|
|
12
|
+
minmaxInt(min, max) {
|
|
13
|
+
min |= 0;
|
|
14
|
+
max |= 0;
|
|
15
|
+
return min + ((this.float() * (max - min)) | 0);
|
|
16
|
+
}
|
|
12
17
|
}
|
package/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export * from "./distributions/uniform.js";
|
|
|
10
10
|
export * from "./pick-random.js";
|
|
11
11
|
export * from "./random-bytes.js";
|
|
12
12
|
export * from "./random-id.js";
|
|
13
|
+
export * from "./sfc32.js";
|
|
13
14
|
export * from "./smush32.js";
|
|
14
15
|
export * from "./system.js";
|
|
15
16
|
export * from "./unique-indices.js";
|
package/index.js
CHANGED
|
@@ -10,6 +10,7 @@ export * from "./distributions/uniform.js";
|
|
|
10
10
|
export * from "./pick-random.js";
|
|
11
11
|
export * from "./random-bytes.js";
|
|
12
12
|
export * from "./random-id.js";
|
|
13
|
+
export * from "./sfc32.js";
|
|
13
14
|
export * from "./smush32.js";
|
|
14
15
|
export * from "./system.js";
|
|
15
16
|
export * from "./unique-indices.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/random",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@microsoft/api-extractor": "^7.23.1",
|
|
44
|
-
"@thi.ng/testament": "^0.2.
|
|
44
|
+
"@thi.ng/testament": "^0.2.7",
|
|
45
45
|
"rimraf": "^3.0.2",
|
|
46
46
|
"tools": "^0.0.1",
|
|
47
47
|
"typedoc": "^0.22.15",
|
|
@@ -119,6 +119,9 @@
|
|
|
119
119
|
"./random-id": {
|
|
120
120
|
"default": "./random-id.js"
|
|
121
121
|
},
|
|
122
|
+
"./sfc32": {
|
|
123
|
+
"default": "./sfc32.js"
|
|
124
|
+
},
|
|
122
125
|
"./smush32": {
|
|
123
126
|
"default": "./smush32.js"
|
|
124
127
|
},
|
|
@@ -153,5 +156,5 @@
|
|
|
153
156
|
"ksuid"
|
|
154
157
|
]
|
|
155
158
|
},
|
|
156
|
-
"gitHead": "
|
|
159
|
+
"gitHead": "e23901b8582af71d8a29e0ce4929f15ac509f9e5\n"
|
|
157
160
|
}
|
package/pick-random.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IRandom } from "./api.js";
|
|
1
2
|
/**
|
|
2
3
|
* Returns a random element from `src` using given {@link IRandom} instance
|
|
3
4
|
* (default: {@link SYSTEM}). The index selection will be constrained to the
|
|
@@ -8,5 +9,12 @@
|
|
|
8
9
|
* @param start -
|
|
9
10
|
* @param end -
|
|
10
11
|
*/
|
|
11
|
-
export declare const pickRandom: <T>(src: ArrayLike<T>, rnd?:
|
|
12
|
+
export declare const pickRandom: <T>(src: ArrayLike<T>, rnd?: IRandom, start?: number, end?: number) => T;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a random key from given `object`.
|
|
15
|
+
*
|
|
16
|
+
* @param obj
|
|
17
|
+
* @param rnd
|
|
18
|
+
*/
|
|
19
|
+
export declare const pickRandomKey: <T>(obj: T, rnd?: IRandom) => keyof T;
|
|
12
20
|
//# sourceMappingURL=pick-random.d.ts.map
|
package/pick-random.js
CHANGED
|
@@ -10,3 +10,10 @@ import { SYSTEM } from "./system.js";
|
|
|
10
10
|
* @param end -
|
|
11
11
|
*/
|
|
12
12
|
export const pickRandom = (src, rnd = SYSTEM, start = 0, end = src.length) => src[rnd.minmax(start, end) | 0];
|
|
13
|
+
/**
|
|
14
|
+
* Returns a random key from given `object`.
|
|
15
|
+
*
|
|
16
|
+
* @param obj
|
|
17
|
+
* @param rnd
|
|
18
|
+
*/
|
|
19
|
+
export const pickRandomKey = (obj, rnd = SYSTEM) => pickRandom(Object.keys(obj), rnd);
|
package/sfc32.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { IBuffered, ICopy } from "@thi.ng/api";
|
|
2
|
+
import type { ISeedable } from "./api.js";
|
|
3
|
+
import { ARandom } from "./arandom.js";
|
|
4
|
+
/**
|
|
5
|
+
* Simple Fast Counter PRNG (32bit version)
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* References:
|
|
9
|
+
* - http://pracrand.sourceforge.net/
|
|
10
|
+
*/
|
|
11
|
+
export declare class SFC32 extends ARandom implements IBuffered<Uint32Array>, ICopy<SFC32>, ISeedable<ArrayLike<number>> {
|
|
12
|
+
buffer: Uint32Array;
|
|
13
|
+
constructor(seed?: ArrayLike<number>);
|
|
14
|
+
copy(): SFC32;
|
|
15
|
+
bytes(): Uint8Array;
|
|
16
|
+
int(): number;
|
|
17
|
+
seed(seed: ArrayLike<number>): this;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=sfc32.d.ts.map
|
package/sfc32.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ARandom } from "./arandom.js";
|
|
2
|
+
import { DEFAULT_SEED_128 } from "./constants.js";
|
|
3
|
+
/**
|
|
4
|
+
* Simple Fast Counter PRNG (32bit version)
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* References:
|
|
8
|
+
* - http://pracrand.sourceforge.net/
|
|
9
|
+
*/
|
|
10
|
+
export class SFC32 extends ARandom {
|
|
11
|
+
constructor(seed = DEFAULT_SEED_128) {
|
|
12
|
+
super();
|
|
13
|
+
this.buffer = new Uint32Array(4);
|
|
14
|
+
this.seed(seed);
|
|
15
|
+
}
|
|
16
|
+
copy() {
|
|
17
|
+
return new SFC32(this.buffer);
|
|
18
|
+
}
|
|
19
|
+
bytes() {
|
|
20
|
+
return new Uint8Array(this.buffer.buffer);
|
|
21
|
+
}
|
|
22
|
+
int() {
|
|
23
|
+
const s = this.buffer;
|
|
24
|
+
const t = (((s[0] + s[1]) >>> 0) + s[3]) >>> 0;
|
|
25
|
+
s[3] = (s[3] + 1) >>> 0;
|
|
26
|
+
s[0] = s[1] ^ (s[1] >>> 9);
|
|
27
|
+
s[1] = (s[2] + (s[2] << 3)) >>> 0;
|
|
28
|
+
s[2] = (((s[2] << 21) | (s[2] >>> 11)) + t) >>> 0;
|
|
29
|
+
return t;
|
|
30
|
+
}
|
|
31
|
+
seed(seed) {
|
|
32
|
+
this.buffer.set(seed);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
}
|
package/weighted-random.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Fn0 } from "@thi.ng/api";
|
|
1
2
|
import type { IRandom } from "./api.js";
|
|
2
3
|
/**
|
|
3
4
|
* Returns a no-arg function which produces a random choice of given weighted
|
|
@@ -18,4 +19,13 @@ import type { IRandom } from "./api.js";
|
|
|
18
19
|
* @param weights - optional weights
|
|
19
20
|
*/
|
|
20
21
|
export declare const weightedRandom: <T>(choices: T[], weights?: ArrayLike<number> | undefined, rnd?: IRandom) => () => T;
|
|
22
|
+
/**
|
|
23
|
+
* Alt version of {@link weightedRandom}, accepting an object of weights
|
|
24
|
+
* instead. The returned function will return keys of given `choices` object,
|
|
25
|
+
* taking into account the weights given for each key.
|
|
26
|
+
*
|
|
27
|
+
* @param choices
|
|
28
|
+
* @param rnd
|
|
29
|
+
*/
|
|
30
|
+
export declare const weightedRandomKey: <T extends Record<string, number>>(choices: T, rnd?: IRandom) => Fn0<keyof T>;
|
|
21
31
|
//# sourceMappingURL=weighted-random.d.ts.map
|
package/weighted-random.js
CHANGED
|
@@ -40,3 +40,15 @@ export const weightedRandom = (choices, weights, rnd = SYSTEM) => {
|
|
|
40
40
|
return undefined;
|
|
41
41
|
};
|
|
42
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Alt version of {@link weightedRandom}, accepting an object of weights
|
|
45
|
+
* instead. The returned function will return keys of given `choices` object,
|
|
46
|
+
* taking into account the weights given for each key.
|
|
47
|
+
*
|
|
48
|
+
* @param choices
|
|
49
|
+
* @param rnd
|
|
50
|
+
*/
|
|
51
|
+
export const weightedRandomKey = (choices, rnd = SYSTEM) => {
|
|
52
|
+
const keys = Object.keys(choices);
|
|
53
|
+
return weightedRandom(keys, keys.map((x) => choices[x]), rnd);
|
|
54
|
+
};
|