@thi.ng/random 3.2.4 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-03-11T12:13:49Z
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.84 KB
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
@@ -4,5 +4,6 @@ export declare abstract class ARandom implements IRandom {
4
4
  float(norm?: number): number;
5
5
  norm(norm?: number): number;
6
6
  minmax(min: number, max: number): number;
7
+ minmaxInt(min: number, max: number): number;
7
8
  }
8
9
  //# sourceMappingURL=arandom.d.ts.map
package/arandom.js CHANGED
@@ -1,4 +1,4 @@
1
- const INV_MAX = 1 / (2 ** 32);
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.2.4",
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",
@@ -34,18 +34,18 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.4",
38
- "@thi.ng/checks": "^3.1.4",
39
- "@thi.ng/errors": "^2.1.4",
40
- "@thi.ng/hex": "^2.1.4"
37
+ "@thi.ng/api": "^8.3.6",
38
+ "@thi.ng/checks": "^3.1.6",
39
+ "@thi.ng/errors": "^2.1.6",
40
+ "@thi.ng/hex": "^2.1.6"
41
41
  },
42
42
  "devDependencies": {
43
- "@microsoft/api-extractor": "^7.19.4",
44
- "@thi.ng/testament": "^0.2.4",
43
+ "@microsoft/api-extractor": "^7.23.1",
44
+ "@thi.ng/testament": "^0.2.7",
45
45
  "rimraf": "^3.0.2",
46
46
  "tools": "^0.0.1",
47
- "typedoc": "^0.22.13",
48
- "typescript": "^4.6.2"
47
+ "typedoc": "^0.22.15",
48
+ "typescript": "^4.6.4"
49
49
  },
50
50
  "keywords": [
51
51
  "binary",
@@ -78,73 +78,76 @@
78
78
  ],
79
79
  "exports": {
80
80
  ".": {
81
- "import": "./index.js"
81
+ "default": "./index.js"
82
82
  },
83
83
  "./api": {
84
- "import": "./api.js"
84
+ "default": "./api.js"
85
85
  },
86
86
  "./arandom": {
87
- "import": "./arandom.js"
87
+ "default": "./arandom.js"
88
88
  },
89
89
  "./coin": {
90
- "import": "./coin.js"
90
+ "default": "./coin.js"
91
91
  },
92
92
  "./constants": {
93
- "import": "./constants.js"
93
+ "default": "./constants.js"
94
94
  },
95
95
  "./crypto": {
96
- "import": "./crypto.js"
96
+ "default": "./crypto.js"
97
97
  },
98
98
  "./distributions/exponential": {
99
- "import": "./distributions/exponential.js"
99
+ "default": "./distributions/exponential.js"
100
100
  },
101
101
  "./distributions/gaussian": {
102
- "import": "./distributions/gaussian.js"
102
+ "default": "./distributions/gaussian.js"
103
103
  },
104
104
  "./distributions/geometric": {
105
- "import": "./distributions/geometric.js"
105
+ "default": "./distributions/geometric.js"
106
106
  },
107
107
  "./distributions/normal": {
108
- "import": "./distributions/normal.js"
108
+ "default": "./distributions/normal.js"
109
109
  },
110
110
  "./distributions/uniform": {
111
- "import": "./distributions/uniform.js"
111
+ "default": "./distributions/uniform.js"
112
112
  },
113
113
  "./pick-random": {
114
- "import": "./pick-random.js"
114
+ "default": "./pick-random.js"
115
115
  },
116
116
  "./random-bytes": {
117
- "import": "./random-bytes.js"
117
+ "default": "./random-bytes.js"
118
118
  },
119
119
  "./random-id": {
120
- "import": "./random-id.js"
120
+ "default": "./random-id.js"
121
+ },
122
+ "./sfc32": {
123
+ "default": "./sfc32.js"
121
124
  },
122
125
  "./smush32": {
123
- "import": "./smush32.js"
126
+ "default": "./smush32.js"
124
127
  },
125
128
  "./system": {
126
- "import": "./system.js"
129
+ "default": "./system.js"
127
130
  },
128
131
  "./unique-indices": {
129
- "import": "./unique-indices.js"
132
+ "default": "./unique-indices.js"
130
133
  },
131
134
  "./uuid": {
132
- "import": "./uuid.js"
135
+ "default": "./uuid.js"
133
136
  },
134
137
  "./weighted-random": {
135
- "import": "./weighted-random.js"
138
+ "default": "./weighted-random.js"
136
139
  },
137
140
  "./xorshift128": {
138
- "import": "./xorshift128.js"
141
+ "default": "./xorshift128.js"
139
142
  },
140
143
  "./xorwow": {
141
- "import": "./xorwow.js"
144
+ "default": "./xorwow.js"
142
145
  },
143
146
  "./xoshiro128": {
144
- "import": "./xoshiro128.js"
147
+ "default": "./xoshiro128.js"
145
148
  },
146
149
  "./xsadd": {
147
- "import": "./xsadd.js"
150
+ "default": "./xsadd.js"
148
151
  }
149
152
  },
150
153
  "thi.ng": {
@@ -153,5 +156,5 @@
153
156
  "ksuid"
154
157
  ]
155
158
  },
156
- "gitHead": "0fc692a3225c068aacafdc4cb6140cf603c67ad8\n"
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?: import("./system.js").SystemRandom, start?: number, end?: number) => T;
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
+ }
@@ -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
@@ -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
+ };