@thi.ng/random 3.6.38 → 3.7.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**: 2024-03-13T14:04:31Z
3
+ - **Last updated**: 2024-03-21T16:11:49Z
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,12 @@ 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.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.7.0) (2024-03-21)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add weightedProbability() ([393dcaa](https://github.com/thi-ng/umbrella/commit/393dcaa))
17
+
12
18
  ### [3.6.13](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.6.13) (2023-11-09)
13
19
 
14
20
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -98,7 +98,7 @@ For Node.js REPL:
98
98
  const random = await import("@thi.ng/random");
99
99
  ```
100
100
 
101
- Package sizes (brotli'd, pre-treeshake): ESM: 1.99 KB
101
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.02 KB
102
102
 
103
103
  ## Dependencies
104
104
 
package/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export * from "./smush32.js";
15
15
  export * from "./system.js";
16
16
  export * from "./unique-indices.js";
17
17
  export * from "./uuid.js";
18
+ export * from "./weighted-probability.js";
18
19
  export * from "./weighted-random.js";
19
20
  export * from "./xorshift128.js";
20
21
  export * from "./xorwow.js";
package/index.js CHANGED
@@ -15,6 +15,7 @@ export * from "./smush32.js";
15
15
  export * from "./system.js";
16
16
  export * from "./unique-indices.js";
17
17
  export * from "./uuid.js";
18
+ export * from "./weighted-probability.js";
18
19
  export * from "./weighted-random.js";
19
20
  export * from "./xorshift128.js";
20
21
  export * from "./xorwow.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/random",
3
- "version": "3.6.38",
3
+ "version": "3.7.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",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@thi.ng/api": "^8.9.30",
40
- "@thi.ng/checks": "^3.5.3",
40
+ "@thi.ng/checks": "^3.5.4",
41
41
  "@thi.ng/errors": "^2.5.1",
42
42
  "@thi.ng/hex": "^2.3.40"
43
43
  },
@@ -135,6 +135,9 @@
135
135
  "./uuid": {
136
136
  "default": "./uuid.js"
137
137
  },
138
+ "./weighted-probability": {
139
+ "default": "./weighted-probability.js"
140
+ },
138
141
  "./weighted-random": {
139
142
  "default": "./weighted-random.js"
140
143
  },
@@ -157,5 +160,5 @@
157
160
  "ksuid"
158
161
  ]
159
162
  },
160
- "gitHead": "bc0f3cb07d6f1cab6dbdc5ff57428f5484e711bb\n"
163
+ "gitHead": "da965520b2f3f8c259a791e8e8864308be2ba0be\n"
161
164
  }
@@ -0,0 +1,70 @@
1
+ import type { FnN } from "@thi.ng/api";
2
+ import type { IRandom } from "./api.js";
3
+ /**
4
+ * Takes a `weight` function and optional {@link IRandom} instance (default:
5
+ * {@link SYSTEM}). The weight function accepts a number in [0,1) interval and
6
+ * returns a number in [0,1]. Draws two random numbers `a` and `b` via
7
+ * {@link IRandom.float} and returns a tuple of `[a, b < weight(a)]`.
8
+ *
9
+ * @example
10
+ * ```ts tangle:../export/weighted-probability.ts
11
+ * import { weightedProbability } from "@thi.ng/random";
12
+ *
13
+ * const [param, success] = weightedProbability((x) => x** 2);
14
+ *
15
+ * console.log(success ? "SUCCESS" : "FAIL", param);
16
+ * ```
17
+ *
18
+ * @example
19
+ * ```ts tangle:../export/weighted-probability-viz.ts
20
+ * import { weightedProbability } from "@thi.ng/random";
21
+ * import { barChartVStr } from "@thi.ng/text-canvas";
22
+ * import {
23
+ * comp, filter, frequencies, map, repeatedly, transduce
24
+ * } from "@thi.ng/transducers";
25
+ *
26
+ * // custom weighting function
27
+ * const weight = (x: number) => Math.sin(x * Math.PI);
28
+ *
29
+ * // draw N samples (`[number, boolean]` tuples)
30
+ * const samples = repeatedly(
31
+ * () => weightedProbability(weight),
32
+ * // number of samples
33
+ * 1e6
34
+ * );
35
+ *
36
+ * // binning helper for histogram
37
+ * const bin = (x: number, n: number) => Math.floor(x * n) / n;
38
+ *
39
+ * // compute histogram, i.e. a Map of `bin => numSuccess` pairs
40
+ * const histogram = transduce(
41
+ * // filter out failures, apply binning
42
+ * comp(filter(x => x[1]), map(x => bin(x[0], 40))),
43
+ * // collect histogram
44
+ * frequencies(),
45
+ * // input samples
46
+ * samples
47
+ * );
48
+ *
49
+ * // visualize as ASCII art bar chart
50
+ * console.log(
51
+ * barChartVStr(10, [...histogram].sort().map(x => x[1]))
52
+ * );
53
+ *
54
+ * // ▃▅▇████▇▅▃
55
+ * // ▂▆████████████▅▁
56
+ * // ▂▇████████████████▆▂
57
+ * // ▅████████████████████▆
58
+ * // ▃████████████████████████▃
59
+ * // ▆██████████████████████████▆
60
+ * // ▁██████████████████████████████▁
61
+ * // ▃████████████████████████████████▃
62
+ * // ▅██████████████████████████████████▅
63
+ * // ▇████████████████████████████████████▇
64
+ * ```
65
+ *
66
+ * @param weight
67
+ * @param rnd
68
+ */
69
+ export declare const weightedProbability: (weight: FnN, rnd?: IRandom) => [number, boolean];
70
+ //# sourceMappingURL=weighted-probability.d.ts.map
@@ -0,0 +1,9 @@
1
+ import { SYSTEM } from "./system.js";
2
+ const weightedProbability = (weight, rnd = SYSTEM) => {
3
+ const a = rnd.float();
4
+ const b = rnd.float();
5
+ return [a, b < weight(a)];
6
+ };
7
+ export {
8
+ weightedProbability
9
+ };