@thi.ng/random 3.5.2 → 3.6.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**: 2023-08-10T12:16:43Z
3
+ - **Last updated**: 2023-08-12T13:14:08Z
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,23 @@ 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.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.6.0) (2023-08-12)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add IRandom.minmaxUint() ([6558eb1](https://github.com/thi-ng/umbrella/commit/6558eb1))
17
+ - clarify .minmaxInt() is for signed (i32)
18
+ - new .minmaxUint() is for unsigned (u32)
19
+ - add ARandom.minmaxUint()
20
+ - add IRandom.probability() ([efdd49c](https://github.com/thi-ng/umbrella/commit/efdd49c))
21
+ - add impl for ARandom base class
22
+
23
+ #### ⏱ Performance improvements
24
+
25
+ - increase Crypto default size to 1KB ([a30075a](https://github.com/thi-ng/umbrella/commit/a30075a))
26
+ - minor update randomBytesFrom() ([770dbe5](https://github.com/thi-ng/umbrella/commit/770dbe5))
27
+ - switch loop direction
28
+
12
29
  ## [3.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.5.0) (2023-07-14)
13
30
 
14
31
  #### 🚀 Features
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.2",
3
+ "version": "3.6.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,14 +34,14 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.9.1",
38
- "@thi.ng/checks": "^3.4.1",
39
- "@thi.ng/errors": "^2.3.1",
40
- "@thi.ng/hex": "^2.3.13"
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.19",
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": "672fb5ede530598da51c9d1cee48fe58c23bad34\n"
159
+ "gitHead": "399f8c53d66b9b763d16c21bb59f145df5f59514\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;