@thi.ng/random 3.4.2 → 3.5.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 +8 -1
- package/README.md +1 -1
- package/package.json +9 -9
- package/unique-indices.d.ts +22 -3
- package/unique-indices.js +21 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-
|
|
3
|
+
- **Last updated**: 2023-08-04T10:58:19Z
|
|
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/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/random",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.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,18 +34,18 @@
|
|
|
34
34
|
"test": "testament test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@thi.ng/api": "^8.
|
|
38
|
-
"@thi.ng/checks": "^3.
|
|
39
|
-
"@thi.ng/errors": "^2.
|
|
40
|
-
"@thi.ng/hex": "^2.3.
|
|
37
|
+
"@thi.ng/api": "^8.9.0",
|
|
38
|
+
"@thi.ng/checks": "^3.4.0",
|
|
39
|
+
"@thi.ng/errors": "^2.3.0",
|
|
40
|
+
"@thi.ng/hex": "^2.3.12"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@microsoft/api-extractor": "^7.
|
|
44
|
-
"@thi.ng/testament": "^0.3.
|
|
43
|
+
"@microsoft/api-extractor": "^7.36.3",
|
|
44
|
+
"@thi.ng/testament": "^0.3.18",
|
|
45
45
|
"rimraf": "^5.0.1",
|
|
46
46
|
"tools": "^0.0.1",
|
|
47
47
|
"typedoc": "^0.24.8",
|
|
48
|
-
"typescript": "^5.1.
|
|
48
|
+
"typescript": "^5.1.6"
|
|
49
49
|
},
|
|
50
50
|
"keywords": [
|
|
51
51
|
"binary",
|
|
@@ -156,5 +156,5 @@
|
|
|
156
156
|
"ksuid"
|
|
157
157
|
]
|
|
158
158
|
},
|
|
159
|
-
"gitHead": "
|
|
159
|
+
"gitHead": "9fa3f7f8169efa30e3c71b43c82f77393581c3b5\n"
|
|
160
160
|
}
|
package/unique-indices.d.ts
CHANGED
|
@@ -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<
|
|
17
|
+
export declare const uniqueValuesFrom: <T>(k: number, fn: Fn0<T>, existing?: T[], maxTrials?: number) => T[];
|
|
15
18
|
/**
|
|
16
|
-
* Similar to (and based
|
|
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
|
|
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);
|