@mark1russell7/rigor-generators 0.1.15 → 0.1.17
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/README.md +43 -0
- package/build/covering.d.ts +54 -0
- package/build/covering.d.ts.map +1 -0
- package/build/covering.js +187 -0
- package/build/covering.js.map +1 -0
- package/build/index.d.ts +3 -2
- package/build/index.js +3 -2
- package/build/random.d.ts +12 -0
- package/build/random.d.ts.map +1 -1
- package/build/random.js +25 -1
- package/build/random.js.map +1 -1
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -49,6 +49,19 @@ stablePick(deriveSeed(7, "origin"), ["tall-row", "ladder", "half-wide"]);
|
|
|
49
49
|
- When a list loses an item, only the draws of that item change.
|
|
50
50
|
- A choice reads one hash for each item, so it costs O(n). `r.pick` costs O(1).
|
|
51
51
|
|
|
52
|
+
## A weighted random order
|
|
53
|
+
|
|
54
|
+
`plackettLuceOrder(seed, items, logWeightOf, keyOf)` gives a random order of a list with the Plackett-Luce law of the weights. It adds a Gumbel draw to the log-weight of each item, and it sorts from the largest sum (the Gumbel-max method):
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
plackettLuceOrder("feed-7", posts, (post) => Math.log(post.score), (post) => post.id);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
- The first item is item `i` with the probability `w_i / sum(w)`. Each next item has the same law over the items that remain.
|
|
61
|
+
- The draw of an item comes from the stream `random(seed).fork(key)`. Thus a new item or a lost item does not change the order of the other items.
|
|
62
|
+
- An item with the log-weight `-Infinity` (the weight 0) comes last. Items with equal sums keep the order of the list.
|
|
63
|
+
- Each key occurs once. A log-weight of NaN or `+Infinity` is a `RangeError`.
|
|
64
|
+
|
|
52
65
|
## Open families of distributions
|
|
53
66
|
|
|
54
67
|
A closed list of size names cannot grow without a change to each corpus. A `DistributionSpec` names a family and its parameters, so a corpus can name any point of the family.
|
|
@@ -130,6 +143,36 @@ The parameters of a dimension come from `r.fork("dimension:" + name)`. The same
|
|
|
130
143
|
|
|
131
144
|
For the mean of a sum of 3 inputs from 50 points, the Latin hypercube gives 0.00042 of the variance of the random design. A test holds the ratio below 1/1000.
|
|
132
145
|
|
|
146
|
+
## Covering arrays
|
|
147
|
+
|
|
148
|
+
A covering array of strength `t` has few rows, but each combination of `t` values of `t` different dimensions is in one row or more.
|
|
149
|
+
The default strength is 2, so each pair of values is in a row. A test of each row then finds each fault that 2 values cause together.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { coveringRows, random, tupleCoverage } from "@mark1russell7/rigor-generators";
|
|
153
|
+
|
|
154
|
+
const dimensions = { shape: ["rect", "circle", "polygon"], count: [1, 10, 100], edges: ["none", "few", "many"], spacing: [0, 4] };
|
|
155
|
+
const rows = coveringRows(dimensions, random(1).fork("pairwise"));
|
|
156
|
+
// 9 rows hold the 45 pairs. The full product has 54 rows.
|
|
157
|
+
tupleCoverage(rows, dimensions).share; // 1
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
- `coveringArray(levels, r, options)` gives the index of a level of each dimension. `coveringRows(dimensions, r, options)` gives the values by name.
|
|
161
|
+
- The option `strength` sets `t`. With fewer dimensions than `t`, the array is the full product.
|
|
162
|
+
- The algorithm is a greedy AETG. It makes `candidates` rows (the default is 50) for each row that it keeps.
|
|
163
|
+
- The arrays are small, but not always the smallest. The row count is at least the product of the `t` largest counts of levels.
|
|
164
|
+
- The draws come from `r`. The same dimensions, options and seed give the same rows.
|
|
165
|
+
- `tupleCoverage(rows, dimensions, t)` gives the share of the combinations that some rows hold, and the list of the missing combinations. It compares the values as text.
|
|
166
|
+
- More than `COVERING_LIMIT` (10 million) combinations is a `RangeError`, not a long run.
|
|
167
|
+
|
|
168
|
+
### Moves from the copies
|
|
169
|
+
|
|
170
|
+
| Copy | Move |
|
|
171
|
+
| --- | --- |
|
|
172
|
+
| `pairwise(dimensions, seed)` of `packages/joint` in `layout-contract` | `coveringRows(dimensions, random(seed).fork("pairwise"))` gives the same rows. A test holds a record of the copy. One dimension gives a row for each value, not no rows. |
|
|
173
|
+
| `coverage(cells, dimensions)` of `packages/joint` in `layout-contract` | `tupleCoverage(cells, dimensions)`. The field `pairs` becomes `tuples`. A missing pair has the same key. |
|
|
174
|
+
| `allPairs` and `pairsOf` of `bench/law-cover.ts` in `packing` | `tupleCoverage`. The key of a pair changes from `area:x+aspect:y` to `area=x&aspect=y`. |
|
|
175
|
+
|
|
133
176
|
## The input of `fnv1a32`
|
|
134
177
|
|
|
135
178
|
The option `input` of `fnv1a32` selects the units that the hash reads. Use it to replace a local copy of FNV-1a with the same hashes.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Covering arrays: few rows that still hold each combination of `t` values of `t` different dimensions.
|
|
3
|
+
* With the strength 2, each pair of values of 2 dimensions is in one row or more (a pairwise array).
|
|
4
|
+
*/
|
|
5
|
+
import { Random } from "./random.js";
|
|
6
|
+
/** The default count of candidate rows for each row that `coveringArray` keeps. */
|
|
7
|
+
export declare const COVERING_CANDIDATES = 50;
|
|
8
|
+
/** The largest count of combinations of values that one array covers. A larger array is an error, not a long run. */
|
|
9
|
+
export declare const COVERING_LIMIT = 1e7;
|
|
10
|
+
/** The options of `coveringArray` and `coveringRows`. */
|
|
11
|
+
export interface CoveringOptions {
|
|
12
|
+
/** The strength `t`: each `t` values of `t` different dimensions are in one row or more. The default is 2. */
|
|
13
|
+
readonly strength?: number;
|
|
14
|
+
/** The count of candidate rows for each row that the array keeps. The default is `COVERING_CANDIDATES`. */
|
|
15
|
+
readonly candidates?: number;
|
|
16
|
+
}
|
|
17
|
+
/** The coverage of a set of rows over some dimensions. */
|
|
18
|
+
export interface Coverage {
|
|
19
|
+
/** The count of the combinations of `t` values of `t` different dimensions. */
|
|
20
|
+
readonly tuples: number;
|
|
21
|
+
/** The count of the combinations that one row or more holds. */
|
|
22
|
+
readonly covered: number;
|
|
23
|
+
/** `covered` divided by `tuples`. Dimensions with no combination give 1. */
|
|
24
|
+
readonly share: number;
|
|
25
|
+
/** The combinations that no row holds, as `<dimension>=<value>&<dimension>=<value>`, in the order of the dimensions. */
|
|
26
|
+
readonly missing: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* This function gives a covering array of strength `t` over dimensions with `levels[i]` levels each. Each row has the index
|
|
30
|
+
* of one level of each dimension. With fewer dimensions than `t`, the array covers each combination of all the dimensions.
|
|
31
|
+
* Thus no dimensions give one empty row, as the product of no sets has one item.
|
|
32
|
+
*
|
|
33
|
+
* The algorithm is a greedy AETG. Each row starts at a level with the most open combinations. The array makes `candidates`
|
|
34
|
+
* rows, and it keeps the row that covers the most new combinations. Each candidate takes the dimensions in a random order.
|
|
35
|
+
* Each dimension takes the level that covers the most new combinations with the dimensions before it.
|
|
36
|
+
* The random draws come from `r`, thus the same levels, options and seed give the same rows.
|
|
37
|
+
*
|
|
38
|
+
* This function throws a `RangeError` for a dimension with no level, for bad options, and for more than `COVERING_LIMIT` combinations.
|
|
39
|
+
*/
|
|
40
|
+
export declare function coveringArray(levels: readonly number[], r: Random, options?: CoveringOptions): number[][];
|
|
41
|
+
/**
|
|
42
|
+
* This function gives a covering array over named dimensions: each row has one value of each dimension. The values of a
|
|
43
|
+
* dimension are its levels, in their order. The options and the draws are those of `coveringArray`. The value of each name
|
|
44
|
+
* in a row has the type of the levels of its dimension. Thus dimensions of different types can be in one call.
|
|
45
|
+
*/
|
|
46
|
+
export declare function coveringRows<D extends Readonly<Record<string, readonly unknown[]>>>(dimensions: D, r: Random, options?: CoveringOptions): { [K in keyof D]: D[K][number]; }[];
|
|
47
|
+
/**
|
|
48
|
+
* This function gives the coverage of some rows over named dimensions, with the strength `t` (the default is 2). A row holds a
|
|
49
|
+
* combination when it has each of its values. The function compares the values as text with `String`, and it ignores the
|
|
50
|
+
* fields of a row that are not dimensions. With fewer dimensions than `t`, the combinations are those of all the dimensions.
|
|
51
|
+
* Thus no dimensions give one empty combination, with the key `""`.
|
|
52
|
+
*/
|
|
53
|
+
export declare function tupleCoverage(rows: readonly Readonly<Record<string, unknown>>[], dimensions: Readonly<Record<string, readonly unknown[]>>, strength?: number): Coverage;
|
|
54
|
+
//# sourceMappingURL=covering.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"covering.d.ts","names":[],"sources":["../src/covering.ts"],"mappings":";;;;;;qBAOa;;qBAGA;;iBAGI;;WAEN;;WAEA;;;iBAIM;;WAEN;;WAEA;;WAEA;;WAEA;;;;;;;;;;;;;;wBA2CK,cAAc,2BAA2B,GAAG,QAAQ,UAAS;;;;;;wBA8G7D,aAAa,UAAU,SAAS,qCAAqC,YAAY,GAAG,GAAG,QAAQ,UAAS,qBAA0B,WAAW,IAAI,EAAE;;;;;;;wBAgBnJ,cAAc,eAAe,SAAS,4BAA4B,YAAY,SAAS,qCAAqC,oBAAe"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// src/covering.ts
|
|
2
|
+
/** The default count of candidate rows for each row that `coveringArray` keeps. */
|
|
3
|
+
const COVERING_CANDIDATES = 50;
|
|
4
|
+
/** The largest count of combinations of values that one array covers. A larger array is an error, not a long run. */
|
|
5
|
+
const COVERING_LIMIT = 1e7;
|
|
6
|
+
const isWhole = (value, least) => typeof value === "number" && Number.isSafeInteger(value) && value >= least;
|
|
7
|
+
/** This function gives each set of `t` indices from 0 to `k - 1`, each set in increasing order, the sets in lexical order. */
|
|
8
|
+
function combinations(k, t) {
|
|
9
|
+
const out = [];
|
|
10
|
+
const pick = [];
|
|
11
|
+
const walk = (from) => {
|
|
12
|
+
if (pick.length === t) {
|
|
13
|
+
out.push([...pick]);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
for (let i = from; i <= k - (t - pick.length); i++) {
|
|
17
|
+
pick.push(i);
|
|
18
|
+
walk(i + 1);
|
|
19
|
+
pick.pop();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
walk(0);
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
/** This function checks the options and gives the strength for `k` dimensions: the given strength, or `k` when there are fewer dimensions. */
|
|
26
|
+
function strengthOf(k, options) {
|
|
27
|
+
const strength = options.strength ?? 2;
|
|
28
|
+
if (!isWhole(strength, 1)) throw new RangeError(`covering: the strength is a whole number of 1 or more, not ${String(strength)}.`);
|
|
29
|
+
return Math.min(strength, k);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* This function gives a covering array of strength `t` over dimensions with `levels[i]` levels each. Each row has the index
|
|
33
|
+
* of one level of each dimension. With fewer dimensions than `t`, the array covers each combination of all the dimensions.
|
|
34
|
+
* Thus no dimensions give one empty row, as the product of no sets has one item.
|
|
35
|
+
*
|
|
36
|
+
* The algorithm is a greedy AETG. Each row starts at a level with the most open combinations. The array makes `candidates`
|
|
37
|
+
* rows, and it keeps the row that covers the most new combinations. Each candidate takes the dimensions in a random order.
|
|
38
|
+
* Each dimension takes the level that covers the most new combinations with the dimensions before it.
|
|
39
|
+
* The random draws come from `r`, thus the same levels, options and seed give the same rows.
|
|
40
|
+
*
|
|
41
|
+
* This function throws a `RangeError` for a dimension with no level, for bad options, and for more than `COVERING_LIMIT` combinations.
|
|
42
|
+
*/
|
|
43
|
+
function coveringArray(levels, r, options = {}) {
|
|
44
|
+
for (const [i, size] of levels.entries()) if (!isWhole(size, 1)) throw new RangeError(`coveringArray: dimension ${i} has ${String(size)} levels, not a whole number of 1 or more.`);
|
|
45
|
+
const t = strengthOf(levels.length, options);
|
|
46
|
+
const candidates = options.candidates ?? 50;
|
|
47
|
+
if (!isWhole(candidates, 1)) throw new RangeError(`coveringArray: candidates is a whole number of 1 or more, not ${String(candidates)}.`);
|
|
48
|
+
const k = levels.length;
|
|
49
|
+
if (k === 0) return [[]];
|
|
50
|
+
const combos = combinations(k, t);
|
|
51
|
+
const comboOf = new Map(combos.map((combo, index) => [combo.join(","), index]));
|
|
52
|
+
const sizes = combos.map((combo) => combo.reduce((product, d) => product * levels[d], 1));
|
|
53
|
+
const total = sizes.reduce((sum, size) => sum + size, 0);
|
|
54
|
+
if (total > 1e7) throw new RangeError(`coveringArray: ${total} combinations of values, more than ${COVERING_LIMIT}.`);
|
|
55
|
+
const open = sizes.map((size) => new Uint8Array(size).fill(1));
|
|
56
|
+
const openBy = levels.map((size, d) => new Array(size).fill(combos.reduce((sum, combo, c) => sum + (combo.includes(d) ? sizes[c] / levels[d] : 0), 0)));
|
|
57
|
+
let left = total;
|
|
58
|
+
const indexOf = (combo, row) => combo.reduce((index, d) => index * levels[d] + row[d], 0);
|
|
59
|
+
const bestLevel = (f, row, done, choose) => {
|
|
60
|
+
const sets = combinations(done.length, t - 1).map((rest) => {
|
|
61
|
+
const combo = [...rest.map((i) => done[i]), f].sort((a, b) => a - b);
|
|
62
|
+
return {
|
|
63
|
+
flags: open[comboOf.get(combo.join(","))],
|
|
64
|
+
combo
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
let top = -1;
|
|
68
|
+
let tops = [];
|
|
69
|
+
for (let v = 0; v < levels[f]; v++) {
|
|
70
|
+
row[f] = v;
|
|
71
|
+
let n = 0;
|
|
72
|
+
for (const { flags, combo } of sets) n += flags[indexOf(combo, row)];
|
|
73
|
+
if (n > top) {
|
|
74
|
+
top = n;
|
|
75
|
+
tops = [];
|
|
76
|
+
}
|
|
77
|
+
if (n === top) tops.push(v);
|
|
78
|
+
}
|
|
79
|
+
return [choose(tops), top];
|
|
80
|
+
};
|
|
81
|
+
const rows = [];
|
|
82
|
+
while (left > 0) {
|
|
83
|
+
let most = -1;
|
|
84
|
+
let starts = [];
|
|
85
|
+
for (let d = 0; d < k; d++) for (let v = 0; v < levels[d]; v++) {
|
|
86
|
+
if (openBy[d][v] > most) {
|
|
87
|
+
most = openBy[d][v];
|
|
88
|
+
starts = [];
|
|
89
|
+
}
|
|
90
|
+
if (openBy[d][v] === most) starts.push([d, v]);
|
|
91
|
+
}
|
|
92
|
+
let best = [];
|
|
93
|
+
let bestGain = -1;
|
|
94
|
+
for (let c = 0; c < candidates; c++) {
|
|
95
|
+
const [first, level] = r.pick(starts);
|
|
96
|
+
const row = new Array(k).fill(-1);
|
|
97
|
+
row[first] = level;
|
|
98
|
+
const done = [first];
|
|
99
|
+
let gain = t === 1 ? open[comboOf.get(String(first))][level] : 0;
|
|
100
|
+
for (const f of r.shuffle([...Array(k).keys()].filter((d) => d !== first))) {
|
|
101
|
+
const [chosen, top] = bestLevel(f, row, done, (ties) => r.pick(ties));
|
|
102
|
+
row[f] = chosen;
|
|
103
|
+
gain += top;
|
|
104
|
+
done.push(f);
|
|
105
|
+
}
|
|
106
|
+
if (gain > bestGain) {
|
|
107
|
+
best = row;
|
|
108
|
+
bestGain = gain;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (bestGain === 0) {
|
|
112
|
+
const c = open.findIndex((flags) => flags.includes(1));
|
|
113
|
+
let index = open[c].indexOf(1);
|
|
114
|
+
best = new Array(k).fill(-1);
|
|
115
|
+
for (const d of [...combos[c]].reverse()) {
|
|
116
|
+
best[d] = index % levels[d];
|
|
117
|
+
index = Math.floor(index / levels[d]);
|
|
118
|
+
}
|
|
119
|
+
const done = [...combos[c]];
|
|
120
|
+
for (let f = 0; f < k; f++) {
|
|
121
|
+
if (done.includes(f)) continue;
|
|
122
|
+
best[f] = bestLevel(f, best, done, (ties) => ties[0])[0];
|
|
123
|
+
done.push(f);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
for (const [c, combo] of combos.entries()) {
|
|
127
|
+
const at = indexOf(combo, best);
|
|
128
|
+
if (open[c][at] === 1) {
|
|
129
|
+
open[c][at] = 0;
|
|
130
|
+
left--;
|
|
131
|
+
for (const d of combo) openBy[d][best[d]]--;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
rows.push(best);
|
|
135
|
+
}
|
|
136
|
+
return rows;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* This function gives a covering array over named dimensions: each row has one value of each dimension. The values of a
|
|
140
|
+
* dimension are its levels, in their order. The options and the draws are those of `coveringArray`. The value of each name
|
|
141
|
+
* in a row has the type of the levels of its dimension. Thus dimensions of different types can be in one call.
|
|
142
|
+
*/
|
|
143
|
+
function coveringRows(dimensions, r, options = {}) {
|
|
144
|
+
const names = Object.keys(dimensions);
|
|
145
|
+
const levels = dimensions;
|
|
146
|
+
return coveringArray(names.map((name) => levels[name].length), r, options).map((row) => Object.fromEntries(names.map((name, i) => [name, levels[name][row[i]]])));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* This function gives the coverage of some rows over named dimensions, with the strength `t` (the default is 2). A row holds a
|
|
150
|
+
* combination when it has each of its values. The function compares the values as text with `String`, and it ignores the
|
|
151
|
+
* fields of a row that are not dimensions. With fewer dimensions than `t`, the combinations are those of all the dimensions.
|
|
152
|
+
* Thus no dimensions give one empty combination, with the key `""`.
|
|
153
|
+
*/
|
|
154
|
+
function tupleCoverage(rows, dimensions, strength = 2) {
|
|
155
|
+
const names = Object.keys(dimensions);
|
|
156
|
+
const t = strengthOf(names.length, { strength });
|
|
157
|
+
const combos = combinations(names.length, t);
|
|
158
|
+
const keyOf = (combo, values) => combo.map((d, i) => `${names[d]}=${String(values[i])}`).join("&");
|
|
159
|
+
let tuples = 0;
|
|
160
|
+
for (const combo of combos) tuples += combo.reduce((product, d) => product * dimensions[names[d]].length, 1);
|
|
161
|
+
if (tuples > 1e7) throw new RangeError(`tupleCoverage: ${tuples} combinations of values, more than ${COVERING_LIMIT}.`);
|
|
162
|
+
const seen = /* @__PURE__ */ new Set();
|
|
163
|
+
for (const row of rows) for (const combo of combos) seen.add(keyOf(combo, combo.map((d) => row[names[d]])));
|
|
164
|
+
const missing = [];
|
|
165
|
+
for (const combo of combos) {
|
|
166
|
+
const values = combo.map((d) => dimensions[names[d]]);
|
|
167
|
+
const walk = (i, chosen) => {
|
|
168
|
+
if (i === combo.length) {
|
|
169
|
+
const key = keyOf(combo, chosen);
|
|
170
|
+
if (!seen.has(key)) missing.push(key);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
for (const value of values[i]) walk(i + 1, [...chosen, value]);
|
|
174
|
+
};
|
|
175
|
+
walk(0, []);
|
|
176
|
+
}
|
|
177
|
+
const covered = tuples - missing.length;
|
|
178
|
+
return {
|
|
179
|
+
tuples,
|
|
180
|
+
covered,
|
|
181
|
+
share: tuples === 0 ? 1 : covered / tuples,
|
|
182
|
+
missing
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
export { COVERING_CANDIDATES, COVERING_LIMIT, coveringArray, coveringRows, tupleCoverage };
|
|
186
|
+
|
|
187
|
+
//# sourceMappingURL=covering.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"covering.js","names":[],"sources":["../src/covering.ts"],"sourcesContent":["/**\n * Covering arrays: few rows that still hold each combination of `t` values of `t` different dimensions.\n * With the strength 2, each pair of values of 2 dimensions is in one row or more (a pairwise array).\n */\nimport type { Random } from \"./random.ts\";\n\n/** The default count of candidate rows for each row that `coveringArray` keeps. */\nexport const COVERING_CANDIDATES = 50;\n\n/** The largest count of combinations of values that one array covers. A larger array is an error, not a long run. */\nexport const COVERING_LIMIT = 10_000_000;\n\n/** The options of `coveringArray` and `coveringRows`. */\nexport interface CoveringOptions {\n /** The strength `t`: each `t` values of `t` different dimensions are in one row or more. The default is 2. */\n readonly strength?: number;\n /** The count of candidate rows for each row that the array keeps. The default is `COVERING_CANDIDATES`. */\n readonly candidates?: number;\n}\n\n/** The coverage of a set of rows over some dimensions. */\nexport interface Coverage {\n /** The count of the combinations of `t` values of `t` different dimensions. */\n readonly tuples: number;\n /** The count of the combinations that one row or more holds. */\n readonly covered: number;\n /** `covered` divided by `tuples`. Dimensions with no combination give 1. */\n readonly share: number;\n /** The combinations that no row holds, as `<dimension>=<value>&<dimension>=<value>`, in the order of the dimensions. */\n readonly missing: readonly string[];\n}\n\nconst isWhole = (value: unknown, least: number): value is number => typeof value === \"number\" && Number.isSafeInteger(value) && value >= least;\n\n/** This function gives each set of `t` indices from 0 to `k - 1`, each set in increasing order, the sets in lexical order. */\nfunction combinations(k: number, t: number): number[][] {\n const out: number[][] = [];\n const pick: number[] = [];\n const walk = (from: number): void => {\n if (pick.length === t) {\n out.push([...pick]);\n return;\n }\n for (let i = from; i <= k - (t - pick.length); i++) {\n pick.push(i);\n walk(i + 1);\n pick.pop();\n }\n };\n walk(0);\n return out;\n}\n\n/** This function checks the options and gives the strength for `k` dimensions: the given strength, or `k` when there are fewer dimensions. */\nfunction strengthOf(k: number, options: CoveringOptions): number {\n const strength = options.strength ?? 2;\n if (!isWhole(strength, 1)) throw new RangeError(`covering: the strength is a whole number of 1 or more, not ${String(strength)}.`);\n return Math.min(strength, k);\n}\n\n/**\n * This function gives a covering array of strength `t` over dimensions with `levels[i]` levels each. Each row has the index\n * of one level of each dimension. With fewer dimensions than `t`, the array covers each combination of all the dimensions.\n * Thus no dimensions give one empty row, as the product of no sets has one item.\n *\n * The algorithm is a greedy AETG. Each row starts at a level with the most open combinations. The array makes `candidates`\n * rows, and it keeps the row that covers the most new combinations. Each candidate takes the dimensions in a random order.\n * Each dimension takes the level that covers the most new combinations with the dimensions before it.\n * The random draws come from `r`, thus the same levels, options and seed give the same rows.\n *\n * This function throws a `RangeError` for a dimension with no level, for bad options, and for more than `COVERING_LIMIT` combinations.\n */\nexport function coveringArray(levels: readonly number[], r: Random, options: CoveringOptions = {}): number[][] {\n for (const [i, size] of levels.entries()) {\n if (!isWhole(size, 1)) throw new RangeError(`coveringArray: dimension ${i} has ${String(size)} levels, not a whole number of 1 or more.`);\n }\n const t = strengthOf(levels.length, options);\n const candidates = options.candidates ?? COVERING_CANDIDATES;\n if (!isWhole(candidates, 1)) throw new RangeError(`coveringArray: candidates is a whole number of 1 or more, not ${String(candidates)}.`);\n const k = levels.length;\n if (k === 0) return [[]];\n\n const combos = combinations(k, t);\n const comboOf = new Map(combos.map((combo, index) => [combo.join(\",\"), index]));\n const sizes = combos.map((combo) => combo.reduce((product, d) => product * levels[d]!, 1));\n const total = sizes.reduce((sum, size) => sum + size, 0);\n if (total > COVERING_LIMIT) throw new RangeError(`coveringArray: ${total} combinations of values, more than ${COVERING_LIMIT}.`);\n // open[c][index] is 1 while the combination `index` of the values of the dimensions `combos[c]` has no row.\n const open = sizes.map((size) => new Uint8Array(size).fill(1));\n // openBy[d][v] is the count of the open combinations that hold level `v` of dimension `d`.\n const openBy = levels.map((size, d) => new Array<number>(size).fill(combos.reduce((sum, combo, c) => sum + (combo.includes(d) ? sizes[c]! / levels[d]! : 0), 0)));\n let left = total;\n const indexOf = (combo: readonly number[], row: readonly number[]): number => combo.reduce((index, d) => index * levels[d]! + row[d]!, 0);\n // The level of dimension `f` that closes the most open combinations with the levels of the row on the dimensions `done`.\n // The gain of a level counts each set of `t - 1` dimensions of `done` with `f`. The function draws among the ties with `choose`.\n const bestLevel = (f: number, row: number[], done: readonly number[], choose: (ties: number[]) => number): [level: number, gain: number] => {\n const sets = combinations(done.length, t - 1).map((rest) => {\n const combo = [...rest.map((i) => done[i]!), f].sort((a, b) => a - b);\n return { flags: open[comboOf.get(combo.join(\",\"))!]!, combo };\n });\n let top = -1;\n let tops: number[] = [];\n for (let v = 0; v < levels[f]!; v++) {\n row[f] = v;\n let n = 0;\n for (const { flags, combo } of sets) n += flags[indexOf(combo, row)]!;\n if (n > top) {\n top = n;\n tops = [];\n }\n if (n === top) tops.push(v);\n }\n return [choose(tops), top];\n };\n\n const rows: number[][] = [];\n while (left > 0) {\n let most = -1;\n let starts: [number, number][] = [];\n for (let d = 0; d < k; d++) {\n for (let v = 0; v < levels[d]!; v++) {\n if (openBy[d]![v]! > most) {\n most = openBy[d]![v]!;\n starts = [];\n }\n if (openBy[d]![v] === most) starts.push([d, v]);\n }\n }\n let best: number[] = [];\n let bestGain = -1;\n for (let c = 0; c < candidates; c++) {\n const [first, level] = r.pick(starts);\n const row = new Array<number>(k).fill(-1);\n row[first] = level;\n const done = [first];\n let gain = t === 1 ? open[comboOf.get(String(first))!]![level]! : 0;\n for (const f of r.shuffle([...Array(k).keys()].filter((d) => d !== first))) {\n const [chosen, top] = bestLevel(f, row, done, (ties) => r.pick(ties));\n row[f] = chosen;\n gain += top;\n done.push(f);\n }\n if (gain > bestGain) {\n best = row;\n bestGain = gain;\n }\n }\n if (bestGain === 0) {\n // With a strength of 3 or more, the greedy rows can miss each open combination. The row then starts from the first open\n // combination, and each other dimension takes its lowest level with the most gain. This row closes one combination or more.\n const c = open.findIndex((flags) => flags.includes(1));\n let index = open[c]!.indexOf(1);\n best = new Array<number>(k).fill(-1);\n for (const d of [...combos[c]!].reverse()) {\n best[d] = index % levels[d]!;\n index = Math.floor(index / levels[d]!);\n }\n const done = [...combos[c]!];\n for (let f = 0; f < k; f++) {\n if (done.includes(f)) continue;\n best[f] = bestLevel(f, best, done, (ties) => ties[0]!)[0];\n done.push(f);\n }\n }\n for (const [c, combo] of combos.entries()) {\n const at = indexOf(combo, best);\n if (open[c]![at] === 1) {\n open[c]![at] = 0;\n left--;\n for (const d of combo) openBy[d]![best[d]!]!--;\n }\n }\n rows.push(best);\n }\n return rows;\n}\n\n/**\n * This function gives a covering array over named dimensions: each row has one value of each dimension. The values of a\n * dimension are its levels, in their order. The options and the draws are those of `coveringArray`. The value of each name\n * in a row has the type of the levels of its dimension. Thus dimensions of different types can be in one call.\n */\nexport function coveringRows<D extends Readonly<Record<string, readonly unknown[]>>>(dimensions: D, r: Random, options: CoveringOptions = {}): { [K in keyof D]: D[K][number] }[] {\n const names = Object.keys(dimensions);\n const levels: Readonly<Record<string, readonly unknown[]>> = dimensions;\n return coveringArray(\n names.map((name) => levels[name]!.length),\n r,\n options,\n ).map((row) => Object.fromEntries(names.map((name, i) => [name, levels[name]![row[i]!]])) as { [K in keyof D]: D[K][number] });\n}\n\n/**\n * This function gives the coverage of some rows over named dimensions, with the strength `t` (the default is 2). A row holds a\n * combination when it has each of its values. The function compares the values as text with `String`, and it ignores the\n * fields of a row that are not dimensions. With fewer dimensions than `t`, the combinations are those of all the dimensions.\n * Thus no dimensions give one empty combination, with the key `\"\"`.\n */\nexport function tupleCoverage(rows: readonly Readonly<Record<string, unknown>>[], dimensions: Readonly<Record<string, readonly unknown[]>>, strength = 2): Coverage {\n const names = Object.keys(dimensions);\n const t = strengthOf(names.length, { strength });\n const combos = combinations(names.length, t);\n const keyOf = (combo: readonly number[], values: readonly unknown[]): string => combo.map((d, i) => `${names[d]}=${String(values[i])}`).join(\"&\");\n let tuples = 0;\n for (const combo of combos) tuples += combo.reduce((product, d) => product * dimensions[names[d]!]!.length, 1);\n if (tuples > COVERING_LIMIT) throw new RangeError(`tupleCoverage: ${tuples} combinations of values, more than ${COVERING_LIMIT}.`);\n const seen = new Set<string>();\n for (const row of rows) {\n for (const combo of combos) seen.add(keyOf(combo, combo.map((d) => row[names[d]!])));\n }\n const missing: string[] = [];\n for (const combo of combos) {\n const values = combo.map((d) => dimensions[names[d]!]!);\n const walk = (i: number, chosen: unknown[]): void => {\n if (i === combo.length) {\n const key = keyOf(combo, chosen);\n if (!seen.has(key)) missing.push(key);\n return;\n }\n for (const value of values[i]!) walk(i + 1, [...chosen, value]);\n };\n walk(0, []);\n }\n const covered = tuples - missing.length;\n return { tuples, covered, share: tuples === 0 ? 1 : covered / tuples, missing };\n}\n"],"mappings":";;AAOA,MAAa,sBAAsB;;AAGnC,MAAa,iBAAiB;AAsB9B,MAAM,WAAW,OAAgB,UAAmC,OAAO,UAAU,YAAY,OAAO,cAAc,KAAK,KAAK,SAAS;;AAGzI,SAAS,aAAa,GAAW,GAAuB;CACtD,MAAM,MAAkB,CAAC;CACzB,MAAM,OAAiB,CAAC;CACxB,MAAM,QAAQ,SAAuB;EACnC,IAAI,KAAK,WAAW,GAAG;GACrB,IAAI,KAAK,CAAC,GAAG,IAAI,CAAC;GAClB;EACF;EACA,KAAK,IAAI,IAAI,MAAM,KAAK,KAAK,IAAI,KAAK,SAAS,KAAK;GAClD,KAAK,KAAK,CAAC;G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eAAe,EAAE;CACjI,MAAM,uBAAO,IAAI,IAAY;CAC7B,KAAK,MAAM,OAAO,MAChB,KAAK,MAAM,SAAS,QAAQ,KAAK,IAAI,MAAM,OAAO,MAAM,KAAK,MAAM,IAAI,MAAM,GAAI,CAAC,CAAC;CAErF,MAAM,UAAoB,CAAC;CAC3B,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,SAAS,MAAM,KAAK,MAAM,WAAW,MAAM,GAAK;EACtD,MAAM,QAAQ,GAAW,WAA4B;GACnD,IAAI,MAAM,MAAM,QAAQ;IACtB,MAAM,MAAM,MAAM,OAAO,MAAM;IAC/B,IAAI,CAAC,KAAK,IAAI,GAAG,GAAG,QAAQ,KAAK,GAAG;IACpC;GACF;GACA,KAAK,MAAM,SAAS,OAAO,IAAK,KAAK,IAAI,GAAG,CAAC,GAAG,QAAQ,KAAK,CAAC;EAChE;EACA,KAAK,GAAG,CAAC,CAAC;CACZ;CACA,MAAM,UAAU,SAAS,QAAQ;CACjC,OAAO;EAAE;EAAQ;EAAS,OAAO,WAAW,IAAI,IAAI,UAAU;EAAQ;CAAQ;AAChF"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { COVERING_CANDIDATES, COVERING_LIMIT, Coverage, CoveringOptions, coveringArray, coveringRows, tupleCoverage } from "./covering.js";
|
|
1
2
|
import { DistributionSpec, Param, dependsOnLibm, sampler } from "./distributions.js";
|
|
2
3
|
import { normalCdf, normalQuantile } from "./normal.js";
|
|
3
4
|
import { Distribution, betaCdf, distributionOf } from "./quantiles.js";
|
|
4
5
|
import { Dimension, InputFamily, Point, SaltelliDesign, familyMap, familyPoints, latinHypercube, randomDesign, saltelliDesign, stratifiedDesign } from "./designs.js";
|
|
5
6
|
import { CanonicalOptions, Sha256Options, canonicalHash, canonicalJson, sha256Hex } from "./hash.js";
|
|
6
7
|
import { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, RecordedTable, recordedSpec } from "./recorded.js";
|
|
7
|
-
import { Random, Source, deriveSeed, fnv1a32, mulberry32, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
|
|
8
|
-
export { type CanonicalOptions, DIAGRAM_BOX_AREA, type Dimension, type Distribution, type DistributionSpec, GIT_BLOB_BYTES, type InputFamily, type Param, type Point, RECORDED_TABLES, type Random, type RecordedTable, type SaltelliDesign, type Sha256Options, type Source, betaCdf, canonicalHash, canonicalJson, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, stablePick, stratifiedDesign };
|
|
8
|
+
import { Random, Source, deriveSeed, fnv1a32, mulberry32, plackettLuceOrder, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
|
|
9
|
+
export { COVERING_CANDIDATES, COVERING_LIMIT, type CanonicalOptions, type Coverage, type CoveringOptions, DIAGRAM_BOX_AREA, type Dimension, type Distribution, type DistributionSpec, GIT_BLOB_BYTES, type InputFamily, type Param, type Point, RECORDED_TABLES, type Random, type RecordedTable, type SaltelliDesign, type Sha256Options, type Source, betaCdf, canonicalHash, canonicalJson, coveringArray, coveringRows, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, plackettLuceOrder, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, stablePick, stratifiedDesign, tupleCoverage };
|
package/build/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { COVERING_CANDIDATES, COVERING_LIMIT, coveringArray, coveringRows, tupleCoverage } from "./covering.js";
|
|
1
2
|
import { dependsOnLibm, sampler } from "./distributions.js";
|
|
2
3
|
import { normalCdf, normalQuantile } from "./normal.js";
|
|
3
4
|
import { betaCdf, distributionOf } from "./quantiles.js";
|
|
4
5
|
import { familyMap, familyPoints, latinHypercube, randomDesign, saltelliDesign, stratifiedDesign } from "./designs.js";
|
|
5
6
|
import { canonicalHash, canonicalJson, sha256Hex } from "./hash.js";
|
|
6
7
|
import { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, recordedSpec } from "./recorded.js";
|
|
7
|
-
import { deriveSeed, fnv1a32, mulberry32, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
|
|
8
|
-
export { DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, betaCdf, canonicalHash, canonicalJson, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, stablePick, stratifiedDesign };
|
|
8
|
+
import { deriveSeed, fnv1a32, mulberry32, plackettLuceOrder, random, randomOf, seedOf, seedsFrom, stablePick } from "./random.js";
|
|
9
|
+
export { COVERING_CANDIDATES, COVERING_LIMIT, DIAGRAM_BOX_AREA, GIT_BLOB_BYTES, RECORDED_TABLES, betaCdf, canonicalHash, canonicalJson, coveringArray, coveringRows, dependsOnLibm, deriveSeed, distributionOf, familyMap, familyPoints, fnv1a32, latinHypercube, mulberry32, normalCdf, normalQuantile, plackettLuceOrder, random, randomDesign, randomOf, recordedSpec, saltelliDesign, sampler, seedOf, seedsFrom, sha256Hex, stablePick, stratifiedDesign, tupleCoverage };
|
package/build/random.d.ts
CHANGED
|
@@ -36,6 +36,18 @@ export declare function deriveSeed(seed: number, label: string): number;
|
|
|
36
36
|
* The function reads one hash for each item, so a choice costs O(n). The list is not empty, and each key occurs once.
|
|
37
37
|
*/
|
|
38
38
|
export declare function stablePick<T>(seed: number, items: readonly T[], keyOf?: (item: T) => string): T;
|
|
39
|
+
/**
|
|
40
|
+
* A random order of a list with the Plackett-Luce law of the weights (the Gumbel-max method). Each item gets the sum of
|
|
41
|
+
* its log-weight and a Gumbel draw. The order goes from the largest sum to the smallest.
|
|
42
|
+
*
|
|
43
|
+
* - The first item is item `i` with the probability `w_i / sum(w)`. Each next item has the same law over the items that remain.
|
|
44
|
+
* - The draw of an item comes from the stream `random(seed).fork(key)`. Thus a new item or a lost item does not change the order of the other items.
|
|
45
|
+
* - Items with equal sums keep the order of the list. Thus the items with the log-weight `-Infinity` come last, in the order of the list.
|
|
46
|
+
*
|
|
47
|
+
* The function reads one draw for each item, and it does not change its inputs. Each key occurs once. A log-weight is
|
|
48
|
+
* finite or `-Infinity`.
|
|
49
|
+
*/
|
|
50
|
+
export declare function plackettLuceOrder<T>(seed: number | string, items: readonly T[], logWeightOf: (item: T) => number, keyOf?: (item: T) => string): T[];
|
|
39
51
|
export interface Random {
|
|
40
52
|
/** The 32-bit seed that the generator started with. Write it in each failure message, so a run can replay. */
|
|
41
53
|
readonly seed: number;
|
package/build/random.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.d.ts","names":[],"sources":["../src/random.ts"],"mappings":";;;;;;;;YASY;;wBAGI,WAAW,eAAe;;;;;;;;;;;wBAqB1B,QAAQ,cAAc;WAAqB;;;wBA+B3C,OAAO;;wBAOP,WAAW,cAAc;;;;;;;;;;wBAazB,WAAW,GAAG,cAAc,gBAAgB,KAAK,SAAQ,MAAM,eAAwB;
|
|
1
|
+
{"version":3,"file":"random.d.ts","names":[],"sources":["../src/random.ts"],"mappings":";;;;;;;;YASY;;wBAGI,WAAW,eAAe;;;;;;;;;;;wBAqB1B,QAAQ,cAAc;WAAqB;;;wBA+B3C,OAAO;;wBAOP,WAAW,cAAc;;;;;;;;;;wBAazB,WAAW,GAAG,cAAc,gBAAgB,KAAK,SAAQ,MAAM,eAAwB;;;;;;;;;;;;wBA0BvF,kBAAkB,GAAG,uBAAuB,gBAAgB,KAAK,cAAc,MAAM,cAAc,SAAQ,MAAM,eAAwB;iBAkBxI;;WAEN;;EAET;;EAEA,IAAI,aAAa;;EAEjB,MAAM;;EAEN,MAAM,aAAa;;EAEnB,OAAO;;EAEP,KAAK,GAAG,gBAAgB,MAAM;;EAE9B,SAAS;;EAET,QAAQ,GAAG,gBAAgB,MAAM;;EAEjC,OAAO,GAAG,gBAAgB,KAAK,YAAY;;EAE3C,OAAO,eAAe;;;;;;;EAOtB,OAAO,mBAAmB;;EAE1B,KAAK,gBAAgB;;;wBAQP,OAAO,uBAAuB,UAAS,iBAAiB,SAAsB;;wBAM9E,SAAS,MAAM,QAAQ,cAAc,UAAS,iBAAiB,SAAsB;;;;;;;;;wBAoFrF,UAAU,2BAA2B"}
|
package/build/random.js
CHANGED
|
@@ -85,6 +85,30 @@ function stablePick(seed, items, keyOf = String) {
|
|
|
85
85
|
}
|
|
86
86
|
return best.item;
|
|
87
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* A random order of a list with the Plackett-Luce law of the weights (the Gumbel-max method). Each item gets the sum of
|
|
90
|
+
* its log-weight and a Gumbel draw. The order goes from the largest sum to the smallest.
|
|
91
|
+
*
|
|
92
|
+
* - The first item is item `i` with the probability `w_i / sum(w)`. Each next item has the same law over the items that remain.
|
|
93
|
+
* - The draw of an item comes from the stream `random(seed).fork(key)`. Thus a new item or a lost item does not change the order of the other items.
|
|
94
|
+
* - Items with equal sums keep the order of the list. Thus the items with the log-weight `-Infinity` come last, in the order of the list.
|
|
95
|
+
*
|
|
96
|
+
* The function reads one draw for each item, and it does not change its inputs. Each key occurs once. A log-weight is
|
|
97
|
+
* finite or `-Infinity`.
|
|
98
|
+
*/
|
|
99
|
+
function plackettLuceOrder(seed, items, logWeightOf, keyOf = String) {
|
|
100
|
+
const root = random(seed);
|
|
101
|
+
const seen = /* @__PURE__ */ new Set();
|
|
102
|
+
const sums = items.map((item) => {
|
|
103
|
+
const key = keyOf(item);
|
|
104
|
+
if (seen.has(key)) throw new RangeError(`plackettLuceOrder: the key ${JSON.stringify(key)} occurs twice.`);
|
|
105
|
+
seen.add(key);
|
|
106
|
+
const logWeight = logWeightOf(item);
|
|
107
|
+
if (Number.isNaN(logWeight) || logWeight === Number.POSITIVE_INFINITY) throw new RangeError(`plackettLuceOrder: the log-weight of the key ${JSON.stringify(key)} is ${logWeight}, and a log-weight is finite or -Infinity.`);
|
|
108
|
+
return logWeight + root.fork(key).gumbel();
|
|
109
|
+
});
|
|
110
|
+
return items.map((_, index) => index).sort((x, y) => sums[y] - sums[x] || x - y).map((index) => items[index]);
|
|
111
|
+
}
|
|
88
112
|
function wholeOrThrow(name, value) {
|
|
89
113
|
if (!Number.isSafeInteger(value)) throw new RangeError(`${name}: ${value} is not a safe integer.`);
|
|
90
114
|
}
|
|
@@ -189,6 +213,6 @@ function seedsFrom(given, defaults) {
|
|
|
189
213
|
return [seed];
|
|
190
214
|
});
|
|
191
215
|
}
|
|
192
|
-
export { deriveSeed, fnv1a32, mulberry32, random, randomOf, seedOf, seedsFrom, stablePick };
|
|
216
|
+
export { deriveSeed, fnv1a32, mulberry32, plackettLuceOrder, random, randomOf, seedOf, seedsFrom, stablePick };
|
|
193
217
|
|
|
194
218
|
//# sourceMappingURL=random.js.map
|
package/build/random.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.js","names":[],"sources":["../src/random.ts"],"sourcesContent":["/**\n * A pseudo-random number generator with a seed, and the draws that tests and benchmarks make from it.\n *\n * The generator is mulberry32. It is fast, it has 32 bits of state and it gives the same sequence for\n * the same seed on each platform. It is not for cryptography. Each repository that copied mulberry32\n * gets the same numbers from `random(seed).next`, so a move to this package keeps the records equal.\n */\n\n/** A source of uniform numbers from 0 (included) to 1 (not included). */\nexport type Source = () => number;\n\n/** The generator mulberry32. The seed is read as a 32-bit unsigned integer. */\nexport function mulberry32(seed: number): Source {\n let state = seed >>> 0;\n return () => {\n state = (state + 0x6d2b79f5) >>> 0;\n let t = state;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n };\n}\n\n/**\n * The FNV-1a hash of 32 bits of a string. The option `input` selects the units that the hash reads:\n *\n * - `\"utf8\"` (the default): the UTF-8 bytes, equal to the bytes of `TextEncoder`. A lone surrogate becomes the bytes of U+FFFD.\n * - `\"utf16\"`: the UTF-16 code units of the string, one unit for each step, from `charCodeAt`.\n * - `\"wtf8\"`: the UTF-8 bytes of each code point. A lone surrogate becomes its own 3 bytes (WTF-8).\n *\n * For a string without a lone surrogate, `\"utf8\"` and `\"wtf8\"` give the same hash. The function uses no API of Node or\n * of the browser, so the hash is the same on each platform.\n */\nexport function fnv1a32(text: string, options?: { readonly input?: \"utf8\" | \"utf16\" | \"wtf8\" }): number {\n const input = options?.input ?? \"utf8\";\n let h = 0x811c9dc5;\n if (input === \"utf16\") {\n for (let i = 0; i < text.length; i++) h = Math.imul(h ^ text.charCodeAt(i), 0x01000193);\n return h >>> 0;\n }\n if (input !== \"utf8\" && input !== \"wtf8\") throw new RangeError(`fnv1a32: input is \"utf8\", \"utf16\" or \"wtf8\", not ${String(input)}.`);\n for (const ch of text) {\n let c = ch.codePointAt(0) as number;\n // A string iterator gives a lone surrogate as one code point. UTF-8 writes U+FFFD in its place, as TextEncoder does.\n if (input === \"utf8\" && c >= 0xd800 && c <= 0xdfff) c = 0xfffd;\n if (c < 0x80) h = Math.imul(h ^ c, 0x01000193);\n else if (c < 0x800) {\n h = Math.imul(h ^ (0xc0 | (c >> 6)), 0x01000193);\n h = Math.imul(h ^ (0x80 | (c & 63)), 0x01000193);\n } else if (c < 0x10000) {\n h = Math.imul(h ^ (0xe0 | (c >> 12)), 0x01000193);\n h = Math.imul(h ^ (0x80 | ((c >> 6) & 63)), 0x01000193);\n h = Math.imul(h ^ (0x80 | (c & 63)), 0x01000193);\n } else {\n h = Math.imul(h ^ (0xf0 | (c >> 18)), 0x01000193);\n h = Math.imul(h ^ (0x80 | ((c >> 12) & 63)), 0x01000193);\n h = Math.imul(h ^ (0x80 | ((c >> 6) & 63)), 0x01000193);\n h = Math.imul(h ^ (0x80 | (c & 63)), 0x01000193);\n }\n }\n return h >>> 0;\n}\n\n/** The 32-bit seed of a number (its low 32 bits, unsigned) or of a string (its FNV-1a hash). */\nexport function seedOf(seed: number | string): number {\n if (typeof seed === \"string\") return fnv1a32(seed);\n if (!Number.isFinite(seed)) throw new RangeError(`seedOf: a seed is a finite number or a string, not ${seed}.`);\n return seed >>> 0;\n}\n\n/** The seed of the stream with the name `label` under `seed`: the hash of `\"<seed>:<label>\"`. */\nexport function deriveSeed(seed: number, label: string): number {\n return fnv1a32(`${seed}:${label}`);\n}\n\n/**\n * One item of a list, chosen by highest random weight: each key has a weight from `seed`, and the largest weight wins.\n *\n * - The order of the list does not change the choice. The smaller key wins a tie of weights.\n * - When a list gains an item, only the draws that the new item wins change. For `n` items, the share of those draws is approximately `1 / (n + 1)`.\n * - When a list loses an item, only the draws of that item change.\n *\n * The function reads one hash for each item, so a choice costs O(n). The list is not empty, and each key occurs once.\n */\nexport function stablePick<T>(seed: number, items: readonly T[], keyOf: (item: T) => string = String): T {\n if (items.length === 0) throw new RangeError(\"stablePick: the list is empty.\");\n const start = seedOf(seed);\n const seen = new Set<string>();\n let best: { item: T; key: string; weight: number } | undefined;\n for (const item of items) {\n const key = keyOf(item);\n if (seen.has(key)) throw new RangeError(`stablePick: the key ${JSON.stringify(key)} occurs twice.`);\n seen.add(key);\n const weight = mulberry32(deriveSeed(start, key))();\n if (best === undefined || weight > best.weight || (weight === best.weight && key < best.key)) best = { item, key, weight };\n }\n return (best as { item: T }).item;\n}\n\nexport interface Random {\n /** The 32-bit seed that the generator started with. Write it in each failure message, so a run can replay. */\n readonly seed: number;\n /** A uniform number from 0 (included) to 1 (not included). */\n next(): number;\n /** A uniform integer from `min` to `max`, both included. */\n int(min: number, max: number): number;\n /** A uniform integer from 0 (included) to `n` (not included). */\n below(n: number): number;\n /** A uniform number from `min` (included) to `max` (not included). */\n range(min: number, max: number): number;\n /** `true` with the probability `p`. */\n chance(p: number): boolean;\n /** One item of a list that is not empty, each with the same probability. */\n pick<T>(items: readonly T[]): T;\n /** The index of a weight, with a probability in proportion to the weight. One draw. */\n weighted(weights: readonly number[]): number;\n /** A new list with the items in a uniform random order (Fisher–Yates, from the end). */\n shuffle<T>(items: readonly T[]): T[];\n /** `k` different items of a list, in the order of the draw (a partial Fisher–Yates). */\n sample<T>(items: readonly T[], k: number): T[];\n /** A normal sample (Box–Muller, two draws). */\n normal(mean?: number, sd?: number): number;\n /**\n * A Gumbel sample with the mode `location` and the scale `scale` (the inverse of the CDF, one draw).\n * The mean is `location + 0.5772 * scale`.\n * Add a Gumbel draw with the scale 1 to the log of each weight, and sort by the sums.\n * The order then has the Plackett-Luce law of the weights (the Gumbel-max method).\n */\n gumbel(location?: number, scale?: number): number;\n /** An independent generator for the part of the work with the name `label`. The same labels give the same streams. */\n fork(label: string): Random;\n}\n\nfunction wholeOrThrow(name: string, value: number): void {\n if (!Number.isSafeInteger(value)) throw new RangeError(`${name}: ${value} is not a safe integer.`);\n}\n\n/** A generator with draws, from a seed. The default source is mulberry32. */\nexport function random(seed: number | string, source: (seed: number) => Source = mulberry32): Random {\n const start = seedOf(seed);\n return randomOf(source(start), start, source);\n}\n\n/** A generator with draws, from a source that you give. `fork` uses `source` for the new streams. */\nexport function randomOf(next: Source, seed: number, source: (seed: number) => Source = mulberry32): Random {\n const below = (n: number): number => {\n wholeOrThrow(\"below\", n);\n if (n < 1) throw new RangeError(`below: n is 1 or more, not ${n}.`);\n return Math.floor(next() * n);\n };\n return {\n seed,\n next,\n int(min, max) {\n wholeOrThrow(\"int\", min);\n wholeOrThrow(\"int\", max);\n if (min > max) throw new RangeError(`int: min ${min} is more than max ${max}.`);\n return min + Math.floor(next() * (max - min + 1));\n },\n below,\n range: (min, max) => min + next() * (max - min),\n chance: (p) => next() < p,\n pick(items) {\n if (items.length === 0) throw new RangeError(\"pick: the list is empty.\");\n return items[below(items.length)] as (typeof items)[number];\n },\n weighted(weights) {\n let total = 0;\n for (const w of weights) {\n if (!(Number.isFinite(w) && w >= 0)) throw new RangeError(`weighted: each weight is finite and 0 or more, not ${w}.`);\n total += w;\n }\n if (!(total > 0)) throw new RangeError(\"weighted: the sum of the weights is 0.\");\n let left = next() * total;\n for (let i = 0; i < weights.length; i++) {\n left -= weights[i] as number;\n if (left < 0) return i;\n }\n // Rounding can leave a small remainder: give the last index with a weight.\n for (let i = weights.length - 1; ; i--) if ((weights[i] as number) > 0) return i;\n },\n shuffle(items) {\n const out = [...items];\n for (let i = out.length - 1; i > 0; i--) {\n const j = below(i + 1);\n [out[i], out[j]] = [out[j] as (typeof out)[number], out[i] as (typeof out)[number]];\n }\n return out;\n },\n sample(items, k) {\n wholeOrThrow(\"sample\", k);\n if (k < 0 || k > items.length) throw new RangeError(`sample: k is from 0 to ${items.length}, not ${k}.`);\n const out = [...items];\n for (let i = 0; i < k; i++) {\n const j = i + below(out.length - i);\n [out[i], out[j]] = [out[j] as (typeof out)[number], out[i] as (typeof out)[number]];\n }\n return out.slice(0, k);\n },\n normal(mean = 0, sd = 1) {\n const u1 = Math.max(next(), 1e-12);\n const u2 = next();\n return mean + sd * Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n },\n gumbel(location = 0, scale = 1) {\n if (!(Number.isFinite(scale) && scale > 0)) throw new RangeError(`gumbel: the scale is finite and more than 0, not ${scale}.`);\n // The same floor as normal: a draw of 0 gives a finite value, not minus infinity.\n const u = Math.max(next(), 1e-12);\n return location - scale * Math.log(-Math.log(u));\n },\n fork(label) {\n const child = deriveSeed(seed, label);\n return randomOf(source(child), child, source);\n },\n };\n}\n\n/** The largest count of seeds that one range gives. A larger range is an error, not a long run. */\nconst MAX_RANGE = 1_000_000;\n\n/**\n * The seeds of a run: the seeds in `given` (the text of an environment variable, for example\n * `process.env.SEED`), else the default seeds. An empty text gives the defaults.\n *\n * The text is one seed (`17`) or a list of seeds and ranges with commas (`1,3,9-11` gives 1, 3, 9, 10\n * and 11), in the order of the list. A range has two whole numbers, the low one first. An empty part, a\n * part that is not an integer and a range with the low end last are each a `RangeError`.\n */\nexport function seedsFrom(given: string | undefined, defaults: readonly number[]): number[] {\n if (given === undefined || given.trim() === \"\") return [...defaults];\n return given.split(\",\").flatMap((raw) => {\n const part = raw.trim();\n const range = /^(\\d+)\\s*-\\s*(\\d+)$/u.exec(part);\n if (range !== null) {\n const [low, high] = [Number(range[1]), Number(range[2])];\n if (!Number.isSafeInteger(high) || high < low || high - low >= MAX_RANGE) {\n throw new RangeError(`seedsFrom: the range ${part} is not low-high with at most ${MAX_RANGE} seeds.`);\n }\n return Array.from({ length: high - low + 1 }, (_, i) => low + i);\n }\n const seed = part === \"\" ? Number.NaN : Number(part);\n if (!Number.isSafeInteger(seed)) throw new RangeError(`seedsFrom: a seed is an integer, not ${part === \"\" ? \"an empty part\" : part}.`);\n return [seed];\n });\n}\n"],"mappings":";;AAYA,SAAgB,WAAW,MAAsB;CAC/C,IAAI,QAAQ,SAAS;CACrB,aAAa;EACX,QAAS,QAAQ,eAAgB;EACjC,IAAI,IAAI;EACR,IAAI,KAAK,KAAK,IAAK,MAAM,IAAK,IAAI,CAAC;EACnC,KAAK,IAAI,KAAK,KAAK,IAAK,MAAM,GAAI,IAAI,EAAE;EACxC,SAAS,IAAK,MAAM,QAAS,KAAK;CACpC;AACF;;;;;;;;;;;AAYA,SAAgB,QAAQ,MAAc,SAAkE;CACtG,MAAM,QAAQ,SAAS,SAAS;CAChC,IAAI,IAAI;CACR,IAAI,UAAU,SAAS;EACrB,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,WAAW,CAAC,GAAG,QAAU;EACtF,OAAO,MAAM;CACf;CACA,IAAI,UAAU,UAAU,UAAU,QAAQ,MAAM,IAAI,WAAW,oDAAoD,OAAO,KAAK,EAAE,EAAE;CACnI,KAAK,MAAM,MAAM,MAAM;EACrB,IAAI,IAAI,GAAG,YAAY,CAAC;EAExB,IA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AI;CACd,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"random.js","names":[],"sources":["../src/random.ts"],"sourcesContent":["/**\n * A pseudo-random number generator with a seed, and the draws that tests and benchmarks make from it.\n *\n * The generator is mulberry32. It is fast, it has 32 bits of state and it gives the same sequence for\n * the same seed on each platform. It is not for cryptography. Each repository that copied mulberry32\n * gets the same numbers from `random(seed).next`, so a move to this package keeps the records equal.\n */\n\n/** A source of uniform numbers from 0 (included) to 1 (not included). */\nexport type Source = () => number;\n\n/** The generator mulberry32. The seed is read as a 32-bit unsigned integer. */\nexport function mulberry32(seed: number): Source {\n let state = seed >>> 0;\n return () => {\n state = (state + 0x6d2b79f5) >>> 0;\n let t = state;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n };\n}\n\n/**\n * The FNV-1a hash of 32 bits of a string. The option `input` selects the units that the hash reads:\n *\n * - `\"utf8\"` (the default): the UTF-8 bytes, equal to the bytes of `TextEncoder`. A lone surrogate becomes the bytes of U+FFFD.\n * - `\"utf16\"`: the UTF-16 code units of the string, one unit for each step, from `charCodeAt`.\n * - `\"wtf8\"`: the UTF-8 bytes of each code point. A lone surrogate becomes its own 3 bytes (WTF-8).\n *\n * For a string without a lone surrogate, `\"utf8\"` and `\"wtf8\"` give the same hash. The function uses no API of Node or\n * of the browser, so the hash is the same on each platform.\n */\nexport function fnv1a32(text: string, options?: { readonly input?: \"utf8\" | \"utf16\" | \"wtf8\" }): number {\n const input = options?.input ?? \"utf8\";\n let h = 0x811c9dc5;\n if (input === \"utf16\") {\n for (let i = 0; i < text.length; i++) h = Math.imul(h ^ text.charCodeAt(i), 0x01000193);\n return h >>> 0;\n }\n if (input !== \"utf8\" && input !== \"wtf8\") throw new RangeError(`fnv1a32: input is \"utf8\", \"utf16\" or \"wtf8\", not ${String(input)}.`);\n for (const ch of text) {\n let c = ch.codePointAt(0) as number;\n // A string iterator gives a lone surrogate as one code point. UTF-8 writes U+FFFD in its place, as TextEncoder does.\n if (input === \"utf8\" && c >= 0xd800 && c <= 0xdfff) c = 0xfffd;\n if (c < 0x80) h = Math.imul(h ^ c, 0x01000193);\n else if (c < 0x800) {\n h = Math.imul(h ^ (0xc0 | (c >> 6)), 0x01000193);\n h = Math.imul(h ^ (0x80 | (c & 63)), 0x01000193);\n } else if (c < 0x10000) {\n h = Math.imul(h ^ (0xe0 | (c >> 12)), 0x01000193);\n h = Math.imul(h ^ (0x80 | ((c >> 6) & 63)), 0x01000193);\n h = Math.imul(h ^ (0x80 | (c & 63)), 0x01000193);\n } else {\n h = Math.imul(h ^ (0xf0 | (c >> 18)), 0x01000193);\n h = Math.imul(h ^ (0x80 | ((c >> 12) & 63)), 0x01000193);\n h = Math.imul(h ^ (0x80 | ((c >> 6) & 63)), 0x01000193);\n h = Math.imul(h ^ (0x80 | (c & 63)), 0x01000193);\n }\n }\n return h >>> 0;\n}\n\n/** The 32-bit seed of a number (its low 32 bits, unsigned) or of a string (its FNV-1a hash). */\nexport function seedOf(seed: number | string): number {\n if (typeof seed === \"string\") return fnv1a32(seed);\n if (!Number.isFinite(seed)) throw new RangeError(`seedOf: a seed is a finite number or a string, not ${seed}.`);\n return seed >>> 0;\n}\n\n/** The seed of the stream with the name `label` under `seed`: the hash of `\"<seed>:<label>\"`. */\nexport function deriveSeed(seed: number, label: string): number {\n return fnv1a32(`${seed}:${label}`);\n}\n\n/**\n * One item of a list, chosen by highest random weight: each key has a weight from `seed`, and the largest weight wins.\n *\n * - The order of the list does not change the choice. The smaller key wins a tie of weights.\n * - When a list gains an item, only the draws that the new item wins change. For `n` items, the share of those draws is approximately `1 / (n + 1)`.\n * - When a list loses an item, only the draws of that item change.\n *\n * The function reads one hash for each item, so a choice costs O(n). The list is not empty, and each key occurs once.\n */\nexport function stablePick<T>(seed: number, items: readonly T[], keyOf: (item: T) => string = String): T {\n if (items.length === 0) throw new RangeError(\"stablePick: the list is empty.\");\n const start = seedOf(seed);\n const seen = new Set<string>();\n let best: { item: T; key: string; weight: number } | undefined;\n for (const item of items) {\n const key = keyOf(item);\n if (seen.has(key)) throw new RangeError(`stablePick: the key ${JSON.stringify(key)} occurs twice.`);\n seen.add(key);\n const weight = mulberry32(deriveSeed(start, key))();\n if (best === undefined || weight > best.weight || (weight === best.weight && key < best.key)) best = { item, key, weight };\n }\n return (best as { item: T }).item;\n}\n\n/**\n * A random order of a list with the Plackett-Luce law of the weights (the Gumbel-max method). Each item gets the sum of\n * its log-weight and a Gumbel draw. The order goes from the largest sum to the smallest.\n *\n * - The first item is item `i` with the probability `w_i / sum(w)`. Each next item has the same law over the items that remain.\n * - The draw of an item comes from the stream `random(seed).fork(key)`. Thus a new item or a lost item does not change the order of the other items.\n * - Items with equal sums keep the order of the list. Thus the items with the log-weight `-Infinity` come last, in the order of the list.\n *\n * The function reads one draw for each item, and it does not change its inputs. Each key occurs once. A log-weight is\n * finite or `-Infinity`.\n */\nexport function plackettLuceOrder<T>(seed: number | string, items: readonly T[], logWeightOf: (item: T) => number, keyOf: (item: T) => string = String): T[] {\n const root = random(seed);\n const seen = new Set<string>();\n const sums = items.map((item) => {\n const key = keyOf(item);\n if (seen.has(key)) throw new RangeError(`plackettLuceOrder: the key ${JSON.stringify(key)} occurs twice.`);\n seen.add(key);\n const logWeight = logWeightOf(item);\n if (Number.isNaN(logWeight) || logWeight === Number.POSITIVE_INFINITY) {\n throw new RangeError(`plackettLuceOrder: the log-weight of the key ${JSON.stringify(key)} is ${logWeight}, and a log-weight is finite or -Infinity.`);\n }\n return logWeight + root.fork(key).gumbel();\n });\n // Two sums of -Infinity give NaN, which is false, so the order of the list decides.\n const order = items.map((_, index) => index).sort((x, y) => (sums[y] as number) - (sums[x] as number) || x - y);\n return order.map((index) => items[index] as T);\n}\n\nexport interface Random {\n /** The 32-bit seed that the generator started with. Write it in each failure message, so a run can replay. */\n readonly seed: number;\n /** A uniform number from 0 (included) to 1 (not included). */\n next(): number;\n /** A uniform integer from `min` to `max`, both included. */\n int(min: number, max: number): number;\n /** A uniform integer from 0 (included) to `n` (not included). */\n below(n: number): number;\n /** A uniform number from `min` (included) to `max` (not included). */\n range(min: number, max: number): number;\n /** `true` with the probability `p`. */\n chance(p: number): boolean;\n /** One item of a list that is not empty, each with the same probability. */\n pick<T>(items: readonly T[]): T;\n /** The index of a weight, with a probability in proportion to the weight. One draw. */\n weighted(weights: readonly number[]): number;\n /** A new list with the items in a uniform random order (Fisher–Yates, from the end). */\n shuffle<T>(items: readonly T[]): T[];\n /** `k` different items of a list, in the order of the draw (a partial Fisher–Yates). */\n sample<T>(items: readonly T[], k: number): T[];\n /** A normal sample (Box–Muller, two draws). */\n normal(mean?: number, sd?: number): number;\n /**\n * A Gumbel sample with the mode `location` and the scale `scale` (the inverse of the CDF, one draw).\n * The mean is `location + 0.5772 * scale`.\n * Add a Gumbel draw with the scale 1 to the log of each weight, and sort by the sums.\n * The order then has the Plackett-Luce law of the weights (the Gumbel-max method).\n */\n gumbel(location?: number, scale?: number): number;\n /** An independent generator for the part of the work with the name `label`. The same labels give the same streams. */\n fork(label: string): Random;\n}\n\nfunction wholeOrThrow(name: string, value: number): void {\n if (!Number.isSafeInteger(value)) throw new RangeError(`${name}: ${value} is not a safe integer.`);\n}\n\n/** A generator with draws, from a seed. The default source is mulberry32. */\nexport function random(seed: number | string, source: (seed: number) => Source = mulberry32): Random {\n const start = seedOf(seed);\n return randomOf(source(start), start, source);\n}\n\n/** A generator with draws, from a source that you give. `fork` uses `source` for the new streams. */\nexport function randomOf(next: Source, seed: number, source: (seed: number) => Source = mulberry32): Random {\n const below = (n: number): number => {\n wholeOrThrow(\"below\", n);\n if (n < 1) throw new RangeError(`below: n is 1 or more, not ${n}.`);\n return Math.floor(next() * n);\n };\n return {\n seed,\n next,\n int(min, max) {\n wholeOrThrow(\"int\", min);\n wholeOrThrow(\"int\", max);\n if (min > max) throw new RangeError(`int: min ${min} is more than max ${max}.`);\n return min + Math.floor(next() * (max - min + 1));\n },\n below,\n range: (min, max) => min + next() * (max - min),\n chance: (p) => next() < p,\n pick(items) {\n if (items.length === 0) throw new RangeError(\"pick: the list is empty.\");\n return items[below(items.length)] as (typeof items)[number];\n },\n weighted(weights) {\n let total = 0;\n for (const w of weights) {\n if (!(Number.isFinite(w) && w >= 0)) throw new RangeError(`weighted: each weight is finite and 0 or more, not ${w}.`);\n total += w;\n }\n if (!(total > 0)) throw new RangeError(\"weighted: the sum of the weights is 0.\");\n let left = next() * total;\n for (let i = 0; i < weights.length; i++) {\n left -= weights[i] as number;\n if (left < 0) return i;\n }\n // Rounding can leave a small remainder: give the last index with a weight.\n for (let i = weights.length - 1; ; i--) if ((weights[i] as number) > 0) return i;\n },\n shuffle(items) {\n const out = [...items];\n for (let i = out.length - 1; i > 0; i--) {\n const j = below(i + 1);\n [out[i], out[j]] = [out[j] as (typeof out)[number], out[i] as (typeof out)[number]];\n }\n return out;\n },\n sample(items, k) {\n wholeOrThrow(\"sample\", k);\n if (k < 0 || k > items.length) throw new RangeError(`sample: k is from 0 to ${items.length}, not ${k}.`);\n const out = [...items];\n for (let i = 0; i < k; i++) {\n const j = i + below(out.length - i);\n [out[i], out[j]] = [out[j] as (typeof out)[number], out[i] as (typeof out)[number]];\n }\n return out.slice(0, k);\n },\n normal(mean = 0, sd = 1) {\n const u1 = Math.max(next(), 1e-12);\n const u2 = next();\n return mean + sd * Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n },\n gumbel(location = 0, scale = 1) {\n if (!(Number.isFinite(scale) && scale > 0)) throw new RangeError(`gumbel: the scale is finite and more than 0, not ${scale}.`);\n // The same floor as normal: a draw of 0 gives a finite value, not minus infinity.\n const u = Math.max(next(), 1e-12);\n return location - scale * Math.log(-Math.log(u));\n },\n fork(label) {\n const child = deriveSeed(seed, label);\n return randomOf(source(child), child, source);\n },\n };\n}\n\n/** The largest count of seeds that one range gives. A larger range is an error, not a long run. */\nconst MAX_RANGE = 1_000_000;\n\n/**\n * The seeds of a run: the seeds in `given` (the text of an environment variable, for example\n * `process.env.SEED`), else the default seeds. An empty text gives the defaults.\n *\n * The text is one seed (`17`) or a list of seeds and ranges with commas (`1,3,9-11` gives 1, 3, 9, 10\n * and 11), in the order of the list. A range has two whole numbers, the low one first. An empty part, a\n * part that is not an integer and a range with the low end last are each a `RangeError`.\n */\nexport function seedsFrom(given: string | undefined, defaults: readonly number[]): number[] {\n if (given === undefined || given.trim() === \"\") return [...defaults];\n return given.split(\",\").flatMap((raw) => {\n const part = raw.trim();\n const range = /^(\\d+)\\s*-\\s*(\\d+)$/u.exec(part);\n if (range !== null) {\n const [low, high] = [Number(range[1]), Number(range[2])];\n if (!Number.isSafeInteger(high) || high < low || high - low >= MAX_RANGE) {\n throw new RangeError(`seedsFrom: the range ${part} is not low-high with at most ${MAX_RANGE} seeds.`);\n }\n return Array.from({ length: high - low + 1 }, (_, i) => low + i);\n }\n const seed = part === \"\" ? Number.NaN : Number(part);\n if (!Number.isSafeInteger(seed)) throw new RangeError(`seedsFrom: a seed is an integer, not ${part === \"\" ? \"an empty part\" : part}.`);\n return [seed];\n });\n}\n"],"mappings":";;AAYA,SAAgB,WAAW,MAAsB;CAC/C,IAAI,QAAQ,SAAS;CACrB,aAAa;EACX,QAAS,QAAQ,eAAgB;EACjC,IAAI,IAAI;EACR,IAAI,KAAK,KAAK,IAAK,MAAM,IAAK,IAAI,CAAC;EACnC,KAAK,IAAI,KAAK,KAAK,IAAK,MAAM,GAAI,IAAI,EAAE;EACxC,SAAS,IAAK,MAAM,QAAS,KAAK;CACpC;AACF;;;;;;;;;;;AAYA,SAAgB,QAAQ,MAAc,SAAkE;CACtG,MAAM,QAAQ,SAAS,SAAS;CAChC,IAAI,IAAI;CACR,IAAI,UAAU,SAAS;EACrB,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,WAAW,CAAC,GAAG,QAAU;EACtF,OAAO,MAAM;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AM,QAAQ,WAAW,MAAM,KAAK;GACpC,OAAO,SAAS,OAAO,KAAK,GAAG,OAAO,MAAM;EAC9C;CACF;AACF;;AAGA,MAAM,YAAY;;;;;;;;;AAUlB,SAAgB,UAAU,OAA2B,UAAuC;CAC1F,IAAI,UAAU,KAAA,KAAa,MAAM,KAAK,MAAM,IAAI,OAAO,CAAC,GAAG,QAAQ;CACnE,OAAO,MAAM,MAAM,GAAG,CAAC,CAAC,SAAS,QAAQ;EACvC,MAAM,OAAO,IAAI,KAAK;EACtB,MAAM,QAAQ,uBAAuB,KAAK,IAAI;EAC9C,IAAI,UAAU,MAAM;GAClB,MAAM,CAAC,KAAK,QAAQ,CAAC,OAAO,MAAM,EAAE,GAAG,OAAO,MAAM,EAAE,CAAC;GACvD,IAAI,CAAC,OAAO,cAAc,IAAI,KAAK,OAAO,OAAO,OAAO,OAAO,WAC7D,MAAM,IAAI,WAAW,wBAAwB,KAAK,gCAAgC,UAAU,QAAQ;GAEtG,OAAO,MAAM,KAAK,EAAE,QAAQ,OAAO,MAAM,EAAE,IAAI,GAAG,MAAM,MAAM,CAAC;EACjE;EACA,MAAM,OAAO,SAAS,KAAK,MAAa,OAAO,IAAI;EACnD,IAAI,CAAC,OAAO,cAAc,IAAI,GAAG,MAAM,IAAI,WAAW,wCAAwC,SAAS,KAAK,kBAAkB,KAAK,EAAE;EACrI,OAAO,CAAC,IAAI;CACd,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/rigor-generators",
|
|
4
|
-
"version": "0.1.
|
|
5
|
-
"description": "A seeded random number generator (mulberry32) with the draws that tests and benchmarks make: integers, picks, weights, shuffles, samples, normal values and named streams. It also gives a content hash: SHA-256 and canonical JSON (RFC 8785).",
|
|
4
|
+
"version": "0.1.17",
|
|
5
|
+
"description": "A seeded random number generator (mulberry32) with the draws that tests and benchmarks make: integers, picks, weights, shuffles, samples, normal values and named streams. It also gives covering arrays (pairwise and of strength t), and a content hash: SHA-256 and canonical JSON (RFC 8785).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"random",
|
|
8
8
|
"seed",
|
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
"deterministic",
|
|
17
17
|
"testing",
|
|
18
18
|
"benchmark",
|
|
19
|
-
"fixtures"
|
|
19
|
+
"fixtures",
|
|
20
|
+
"pairwise",
|
|
21
|
+
"covering-array",
|
|
22
|
+
"combinatorial-testing"
|
|
20
23
|
],
|
|
21
24
|
"homepage": "https://github.com/mark1russell7/rigor/tree/main/packages/generators#readme",
|
|
22
25
|
"bugs": {
|
|
@@ -43,8 +46,10 @@
|
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
48
|
"@mark1russell7/cue": "^2.0.0",
|
|
49
|
+
"@mark1russell7/rigor-testkit": "^0.1.10",
|
|
46
50
|
"@types/node": "^26.6.5",
|
|
47
51
|
"esbuild": "^0.28.2",
|
|
52
|
+
"fast-check": "^4.10.2",
|
|
48
53
|
"tsdown": "^0.23.1",
|
|
49
54
|
"typescript": "^7.0.2",
|
|
50
55
|
"vitest": "^5.0.3"
|