@thi.ng/random 3.5.3 → 3.6.1

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**: 2023-08-10T12:25:09Z
3
+ - **Last updated**: 2023-08-12T16:14:35Z
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,30 @@ 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.6.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.6.1) (2023-08-12)
13
+
14
+ #### ⏱ Performance improvements
15
+
16
+ - add/re-use internal buf for uuid() ([6bf7f1d](https://github.com/thi-ng/umbrella/commit/6bf7f1d))
17
+ - avoid temp allocations (10% faster)
18
+
19
+ ## [3.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.6.0) (2023-08-12)
20
+
21
+ #### 🚀 Features
22
+
23
+ - add IRandom.minmaxUint() ([6558eb1](https://github.com/thi-ng/umbrella/commit/6558eb1))
24
+ - clarify .minmaxInt() is for signed (i32)
25
+ - new .minmaxUint() is for unsigned (u32)
26
+ - add ARandom.minmaxUint()
27
+ - add IRandom.probability() ([efdd49c](https://github.com/thi-ng/umbrella/commit/efdd49c))
28
+ - add impl for ARandom base class
29
+
30
+ #### ⏱ Performance improvements
31
+
32
+ - increase Crypto default size to 1KB ([a30075a](https://github.com/thi-ng/umbrella/commit/a30075a))
33
+ - minor update randomBytesFrom() ([770dbe5](https://github.com/thi-ng/umbrella/commit/770dbe5))
34
+ - switch loop direction
35
+
12
36
  ## [3.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.5.0) (2023-07-14)
13
37
 
14
38
  #### 🚀 Features
package/README.md CHANGED
@@ -94,7 +94,7 @@ For Node.js REPL:
94
94
  const random = await import("@thi.ng/random");
95
95
  ```
96
96
 
97
- Package sizes (brotli'd, pre-treeshake): ESM: 1.86 KB
97
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.87 KB
98
98
 
99
99
  ## Dependencies
100
100
 
package/api.d.ts CHANGED
@@ -14,7 +14,7 @@ export interface INorm {
14
14
  norm(scale?: number): number;
15
15
  /**
16
16
  * Similar to {@link INorm.norm}, but returns values in either the
17
- * `[min..max)` or in the `(-max...min]` interval (i.e. excluding values in
17
+ * `[min..max)` or in the `(-max...-min]` interval (i.e. excluding values in
18
18
  * the `(-min..min)` range). Both `min` and `max` MUST be >= 0.
19
19
  *
20
20
  * @remarks
@@ -42,6 +42,13 @@ export interface IRandom extends INorm {
42
42
  * @param max - default 1
43
43
  */
44
44
  float(max?: number): number;
45
+ /**
46
+ * Calls {@link IRandom.float} and returns true iff result is < `p`
47
+ * (assumed to be in [0..1] interval).
48
+ *
49
+ * @param p
50
+ */
51
+ probability(p: number): boolean;
45
52
  /**
46
53
  * Returns float in [min..max) interval.
47
54
  *
@@ -53,7 +60,7 @@ export interface IRandom extends INorm {
53
60
  */
54
61
  minmax(min: number, max: number): number;
55
62
  /**
56
- * Returns int in [min..max) interval.
63
+ * Returns int in **signed** integer [min..max) interval.
57
64
  *
58
65
  * @remarks
59
66
  * See: https://github.com/thi-ng/umbrella/wiki/Glossary#interval
@@ -62,6 +69,16 @@ export interface IRandom extends INorm {
62
69
  * @param max -
63
70
  */
64
71
  minmaxInt(min: number, max: number): number;
72
+ /**
73
+ * Returns int in **unsigned** integer [min..max) interval.
74
+ *
75
+ * @remarks
76
+ * See: https://github.com/thi-ng/umbrella/wiki/Glossary#interval
77
+ *
78
+ * @param min -
79
+ * @param max -
80
+ */
81
+ minmaxUint(min: number, max: number): number;
65
82
  }
66
83
  export interface ISeedable<T> {
67
84
  /**
package/arandom.d.ts CHANGED
@@ -2,9 +2,11 @@ import type { IRandom } from "./api.js";
2
2
  export declare abstract class ARandom implements IRandom {
3
3
  abstract int(): number;
4
4
  float(norm?: number): number;
5
+ probability(p: number): boolean;
5
6
  norm(norm?: number): number;
6
7
  normMinMax(min: number, max: number): number;
7
8
  minmax(min: number, max: number): number;
8
9
  minmaxInt(min: number, max: number): number;
10
+ minmaxUint(min: number, max: number): number;
9
11
  }
10
12
  //# sourceMappingURL=arandom.d.ts.map
package/arandom.js CHANGED
@@ -3,6 +3,9 @@ export class ARandom {
3
3
  float(norm = 1) {
4
4
  return this.int() * INV_MAX * norm;
5
5
  }
6
+ probability(p) {
7
+ return this.float() < p;
8
+ }
6
9
  norm(norm = 1) {
7
10
  return (this.int() * INV_MAX - 0.5) * 2 * norm;
8
11
  }
@@ -15,7 +18,10 @@ export class ARandom {
15
18
  }
16
19
  minmaxInt(min, max) {
17
20
  min |= 0;
18
- max |= 0;
19
- return min + ((this.float() * (max - min)) | 0);
21
+ return min + (this.int() % ((max | 0) - min));
22
+ }
23
+ minmaxUint(min, max) {
24
+ min >>>= 0;
25
+ return min + (this.int() % ((max >>> 0) - min));
20
26
  }
21
27
  }
package/coin.d.ts CHANGED
@@ -4,7 +4,7 @@ import type { IRandom } from "./api.js";
4
4
  * quality of given {@link IRandom}) PRNG.
5
5
  *
6
6
  * @remarks
7
- * Also see {@link fairCoin}.
7
+ * Also see {@link fairCoin} and {@link IRandom.probability}.
8
8
  *
9
9
  * @param rnd -
10
10
  */
package/coin.js CHANGED
@@ -4,7 +4,7 @@ import { SYSTEM } from "./system.js";
4
4
  * quality of given {@link IRandom}) PRNG.
5
5
  *
6
6
  * @remarks
7
- * Also see {@link fairCoin}.
7
+ * Also see {@link fairCoin} and {@link IRandom.probability}.
8
8
  *
9
9
  * @param rnd -
10
10
  */
package/crypto.js CHANGED
@@ -14,7 +14,7 @@ export class Crypto extends ARandom {
14
14
  /**
15
15
  * @param size - buffer size in bytes (will be rounded to next multiple of 4)
16
16
  */
17
- constructor(size = 64) {
17
+ constructor(size = 1024) {
18
18
  super();
19
19
  this.buffer = new Uint8Array((size + 3) & ~3);
20
20
  this.u32 = new Uint32Array(this.buffer.buffer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/random",
3
- "version": "3.5.3",
3
+ "version": "3.6.1",
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,14 +34,14 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.9.2",
38
- "@thi.ng/checks": "^3.4.2",
39
- "@thi.ng/errors": "^2.3.2",
40
- "@thi.ng/hex": "^2.3.14"
37
+ "@thi.ng/api": "^8.9.3",
38
+ "@thi.ng/checks": "^3.4.3",
39
+ "@thi.ng/errors": "^2.3.3",
40
+ "@thi.ng/hex": "^2.3.15"
41
41
  },
42
42
  "devDependencies": {
43
- "@microsoft/api-extractor": "^7.36.3",
44
- "@thi.ng/testament": "^0.3.20",
43
+ "@microsoft/api-extractor": "^7.36.4",
44
+ "@thi.ng/testament": "^0.3.21",
45
45
  "rimraf": "^5.0.1",
46
46
  "tools": "^0.0.1",
47
47
  "typedoc": "^0.24.8",
@@ -156,5 +156,5 @@
156
156
  "ksuid"
157
157
  ]
158
158
  },
159
- "gitHead": "ad9ac3232c6fc5fc8a0df75ac82fc1e0e9fb0258\n"
159
+ "gitHead": "4154283d772824d4b57d4b7ab97be22a6e1ffdd7\n"
160
160
  }
package/random-bytes.js CHANGED
@@ -10,7 +10,7 @@ import { SYSTEM } from "./system.js";
10
10
  * @param end -
11
11
  */
12
12
  export const randomBytesFrom = (rnd, buf, start = 0, end = buf.length) => {
13
- for (let i = end; --i >= start;) {
13
+ for (let i = start; i < end; i++) {
14
14
  buf[i] = rnd.int() & 0xff;
15
15
  }
16
16
  return buf;
package/uuid.js CHANGED
@@ -15,6 +15,8 @@ export const uuidv4Bytes = (buf, rnd) => {
15
15
  buf[8] = 0x80 | (buf[8] & 0x3f);
16
16
  return buf;
17
17
  };
18
+ /** @internal */
19
+ const __buf = new Uint8Array(16);
18
20
  /**
19
21
  * Returns a UUID string, either from given byte array, or if omitted, using a
20
22
  * new UUID v4 produced by {@link uuidv4Bytes}.
@@ -22,4 +24,4 @@ export const uuidv4Bytes = (buf, rnd) => {
22
24
  * @param id - byte array
23
25
  * @param i - start index
24
26
  */
25
- export const uuid = (id, i = 0) => $uuid(id || uuidv4Bytes(), i);
27
+ export const uuid = (id, i = 0) => $uuid(id || uuidv4Bytes(__buf), i);