@thi.ng/random 3.4.2 → 3.5.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-06-14T07:58:51Z
3
+ - **Last updated**: 2023-07-14T11:37:51Z
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.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.5.0) (2023-07-14)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add pickRandomUnique() ([a70c951](https://github.com/thi-ng/umbrella/commit/a70c951))
17
+ - add generics for uniqueValuesFrom()
18
+
12
19
  ## [3.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/random@3.4.0) (2023-04-19)
13
20
 
14
21
  #### 🚀 Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/random",
3
- "version": "3.4.2",
3
+ "version": "3.5.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",
@@ -156,5 +156,5 @@
156
156
  "ksuid"
157
157
  ]
158
158
  },
159
- "gitHead": "54e825a976c72067a244cff8725ca98f5416d26d\n"
159
+ "gitHead": "88cfe77770f3b07c788301dccefcb9547cd4aff6\n"
160
160
  }
@@ -6,15 +6,18 @@ import type { IRandom } from "./api.js";
6
6
  * samples (or creates a new one). Returns the array. Gives up after
7
7
  * `maxTrials`.
8
8
  *
9
+ * @remarks
10
+ * Internally uses `Array.includes()` to check for duplicates.
11
+ *
9
12
  * @param k -
10
13
  * @param fn -
11
14
  * @param existing -
12
15
  * @param maxTrials -
13
16
  */
14
- export declare const uniqueValuesFrom: (k: number, fn: Fn0<number>, existing?: number[], maxTrials?: number) => number[];
17
+ export declare const uniqueValuesFrom: <T>(k: number, fn: Fn0<T>, existing?: T[], maxTrials?: number) => T[];
15
18
  /**
16
- * Similar to (and based o) {@link uniqueValuesFrom}. Attempts to add `k` unique
17
- * integer indices in the `[0, max)` interval to the (optional) array of
19
+ * Similar to (and based on) {@link uniqueValuesFrom}. Attempts to add `k`
20
+ * unique integer indices in the `[0, max)` interval to the (optional) array of
18
21
  * pre-`existing` indices (which will never be picked again and new indices will
19
22
  * be added to). Returns updated array.
20
23
  *
@@ -29,4 +32,20 @@ export declare const uniqueValuesFrom: (k: number, fn: Fn0<number>, existing?: n
29
32
  * @param rnd -
30
33
  */
31
34
  export declare const uniqueIndices: (k: number, max: number, existing?: number[], maxTrials?: number, rnd?: IRandom) => number[];
35
+ /**
36
+ * Combination of {@link pickRandom} and {@link uniqueValuesFrom}. Picks up to
37
+ * `k` unique values from `src` array (each with `maxTrials`) and adds them to
38
+ * given `existing` array (or creates a new one by default) and returns it. Uses
39
+ * given `rnd` instance (default: {@link SYSTEM} to randomly select candidates.
40
+ *
41
+ * @remarks
42
+ * Internally uses `Array.includes()` to check for duplicates.
43
+ *
44
+ * @param k
45
+ * @param src
46
+ * @param existing
47
+ * @param maxTrials
48
+ * @param rnd
49
+ */
50
+ export declare const pickRandomUnique: <T>(k: number, src: T[], existing?: T[] | undefined, maxTrials?: number, rnd?: IRandom) => T[];
32
51
  //# sourceMappingURL=unique-indices.d.ts.map
package/unique-indices.js CHANGED
@@ -6,6 +6,9 @@ import { SYSTEM } from "./system.js";
6
6
  * samples (or creates a new one). Returns the array. Gives up after
7
7
  * `maxTrials`.
8
8
  *
9
+ * @remarks
10
+ * Internally uses `Array.includes()` to check for duplicates.
11
+ *
9
12
  * @param k -
10
13
  * @param fn -
11
14
  * @param existing -
@@ -27,8 +30,8 @@ export const uniqueValuesFrom = (k, fn, existing = [], maxTrials = 100) => {
27
30
  return existing;
28
31
  };
29
32
  /**
30
- * Similar to (and based o) {@link uniqueValuesFrom}. Attempts to add `k` unique
31
- * integer indices in the `[0, max)` interval to the (optional) array of
33
+ * Similar to (and based on) {@link uniqueValuesFrom}. Attempts to add `k`
34
+ * unique integer indices in the `[0, max)` interval to the (optional) array of
32
35
  * pre-`existing` indices (which will never be picked again and new indices will
33
36
  * be added to). Returns updated array.
34
37
  *
@@ -46,3 +49,19 @@ export const uniqueIndices = (k, max, existing, maxTrials = max, rnd = SYSTEM) =
46
49
  assert(k >= 0 && k <= max, `k must be in [0, ${max}] interval`);
47
50
  return uniqueValuesFrom(k, () => rnd.int() % max, existing, maxTrials);
48
51
  };
52
+ /**
53
+ * Combination of {@link pickRandom} and {@link uniqueValuesFrom}. Picks up to
54
+ * `k` unique values from `src` array (each with `maxTrials`) and adds them to
55
+ * given `existing` array (or creates a new one by default) and returns it. Uses
56
+ * given `rnd` instance (default: {@link SYSTEM} to randomly select candidates.
57
+ *
58
+ * @remarks
59
+ * Internally uses `Array.includes()` to check for duplicates.
60
+ *
61
+ * @param k
62
+ * @param src
63
+ * @param existing
64
+ * @param maxTrials
65
+ * @param rnd
66
+ */
67
+ export const pickRandomUnique = (k, src, existing, maxTrials = 100, rnd = SYSTEM) => uniqueValuesFrom(k, () => src[rnd.int() % src.length], existing, maxTrials);