@zukhruf/random 0.0.0-stage → 0.8.276
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/LICENSE +21 -0
- package/NOTICE +18 -0
- package/README.md +116 -2
- package/dist/bit-generator/bit-generator.d.ts +5 -0
- package/dist/bit-generator/bit-generator.d.ts.map +1 -0
- package/dist/bit-generator/bit-generator.js +2 -0
- package/dist/bit-generator/bit-generator.js.map +1 -0
- package/dist/bit-generator/mt19937.d.ts +9 -0
- package/dist/bit-generator/mt19937.d.ts.map +1 -0
- package/dist/bit-generator/mt19937.js +59 -0
- package/dist/bit-generator/mt19937.js.map +1 -0
- package/dist/bit-generator/pcg64.d.ts +9 -0
- package/dist/bit-generator/pcg64.d.ts.map +1 -0
- package/dist/bit-generator/pcg64.js +50 -0
- package/dist/bit-generator/pcg64.js.map +1 -0
- package/dist/generator/bounded-lemire.d.ts +3 -0
- package/dist/generator/bounded-lemire.d.ts.map +1 -0
- package/dist/generator/bounded-lemire.js +17 -0
- package/dist/generator/bounded-lemire.js.map +1 -0
- package/dist/generator/default-rng.d.ts +3 -0
- package/dist/generator/default-rng.d.ts.map +1 -0
- package/dist/generator/default-rng.js +9 -0
- package/dist/generator/default-rng.js.map +1 -0
- package/dist/generator/generator.d.ts +17 -0
- package/dist/generator/generator.d.ts.map +1 -0
- package/dist/generator/generator.js +97 -0
- package/dist/generator/generator.js.map +1 -0
- package/dist/generator/random-interval.d.ts +3 -0
- package/dist/generator/random-interval.d.ts.map +1 -0
- package/dist/generator/random-interval.js +13 -0
- package/dist/generator/random-interval.js.map +1 -0
- package/dist/generator/random-state.d.ts +21 -0
- package/dist/generator/random-state.d.ts.map +1 -0
- package/dist/generator/random-state.js +123 -0
- package/dist/generator/random-state.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/seed/seed-sequence.d.ts +6 -0
- package/dist/seed/seed-sequence.d.ts.map +1 -0
- package/dist/seed/seed-sequence.js +74 -0
- package/dist/seed/seed-sequence.js.map +1 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ezzabuzaid
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
@zukhruf/random
|
|
2
|
+
Copyright 2026 ezzabuzaid
|
|
3
|
+
|
|
4
|
+
This package ports the semantics of numpy.random in NumPy 2.5.4:
|
|
5
|
+
SeedSequence (bit_generator.pyx), PCG64 with the XSL-RR output, the MT19937
|
|
6
|
+
of RandomState with its init_genrand seeding, the draws random_interval,
|
|
7
|
+
random_bounded_uint64 and buffered_bounded_lemire_uint32 of distributions.c,
|
|
8
|
+
and the shuffle, permutation, choice, randint and random_sample of
|
|
9
|
+
_generator.pyx and mtrand.pyx. The package does not include NumPy's code.
|
|
10
|
+
|
|
11
|
+
- NumPy: Copyright (c) 2005-2025, NumPy Developers, BSD 3-Clause License,
|
|
12
|
+
https://github.com/numpy/numpy.
|
|
13
|
+
- numpy/random: Copyright (c) 2019 Kevin Sheppard, dual-licensed under the
|
|
14
|
+
University of Illinois/NCSA Open Source License and the BSD 3-Clause
|
|
15
|
+
License.
|
|
16
|
+
- MT19937: Copyright (C) 1997-2002, Makoto Matsumoto and Takuji Nishimura,
|
|
17
|
+
and Copyright (c) 2003-2005, Jean-Sebastien Roy, BSD 3-Clause License.
|
|
18
|
+
- PCG64: Copyright 2014 Melissa O'Neill, MIT License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zukhruf/random
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Status: in use.** The eval of relevance-filtering draws from this package, and its tests prove it against NumPy 2.5.4. The decisions are in [ADR 0001](docs/adr/0001-one-package-for-the-generators-of-numpy-random.md).
|
|
4
|
+
|
|
5
|
+
The generators of `numpy.random` in JavaScript, to the last bit. Use it when a TypeScript port must draw the same numbers as a Python program that seeds NumPy, such as the bootstrap of a benchmark or the split of a dataset.
|
|
6
|
+
|
|
7
|
+
The package is on npm, so a program outside zukhruf can seed `@zukhruf/evals`' paired bootstrap with it. The words are defined in [CONTEXT.md](CONTEXT.md).
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @zukhruf/random
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Use it
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { RandomState, defaultRng } from '@zukhruf/random';
|
|
17
|
+
|
|
18
|
+
const generator = defaultRng(42);
|
|
19
|
+
generator.integers(0, 10, 5); // [0, 7, 6, 4, 4]
|
|
20
|
+
generator.permutation(5); // [0, 3, 4, 1, 2]
|
|
21
|
+
generator.choice([10, 20, 30, 40, 50], 2, { replace: false }); // [10, 30]
|
|
22
|
+
generator.random(2); // [0.761139701990353, 0.7860643052769538]
|
|
23
|
+
generator.choice(10, 3); // [5, 1, 8]
|
|
24
|
+
generator.choice([10, 20, 30, 40, 50], 3); // [30, 30, 20]
|
|
25
|
+
|
|
26
|
+
defaultRng(2n ** 70n + 3n).integers(0, 1000, 3); // [141, 420, 270]
|
|
27
|
+
|
|
28
|
+
const legacy = new RandomState(42);
|
|
29
|
+
legacy.randint(0, 10, 5); // [6, 3, 7, 4, 6]
|
|
30
|
+
legacy.randomSample(2); // [0.44583275285359114, 0.09997491581800289]
|
|
31
|
+
legacy.choice(3, 4, { p: [0.5, 0.25, 0.25] }); // [0, 0, 0, 1]
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Each value in the comments is what NumPy 2.5.4 gives for the same calls in the same order (`numpy.random.default_rng(42)`, `numpy.random.RandomState(42)`).
|
|
35
|
+
|
|
36
|
+
## The parts
|
|
37
|
+
|
|
38
|
+
`RandomState` moves here from mteb with its name. `NumpyGenerator` becomes `Generator` and `defaultRng`. The other exports are new. Each class keeps the name of its NumPy class, so a reader finds the source of each rule in NumPy.
|
|
39
|
+
|
|
40
|
+
| Export | NumPy | What it does |
|
|
41
|
+
| -------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
42
|
+
| `SeedSequence` | `numpy.random.SeedSequence` | `new SeedSequence(seed)` mixes the seed into a pool of 4 words of 32 bits. `generateState(nWords)` gives `nWords` words of 32 bits from the pool, as `generate_state(n_words, dtype=np.uint32)` gives them. |
|
|
43
|
+
| `BitGenerator` | `numpy.random.BitGenerator` (the `bitgen_t` of C) | An interface: `nextUint32()` gives the next 32 bits of the stream, and `nextDouble()` gives the next float of 53 bits in [0, 1) by the rule of the bit generator (`next_double`). `Generator` takes any bit generator. |
|
|
44
|
+
| `PCG64` | `numpy.random.PCG64` | PCG64 with the XSL-RR output: 128 bits of state, 64 bits for each step. `new PCG64(seed)` or `new PCG64(seedSequence)`. `nextUint32()` gives the low half of a 64-bit output and keeps the high half for the next call, as NumPy keeps it in the bit generator. So two draws that share one generator share that half. `nextDouble()` takes the high 53 bits of a new 64-bit output and keeps the buffered half for the next `nextUint32()`. |
|
|
45
|
+
| `MT19937` | `numpy.random.MT19937`, `_legacy_seeding` | The Mersenne Twister. `MT19937.legacy(seed)` seeds it with `init_genrand`, as `RandomState(seed)` seeds it. The seed is from 0 to 2^32 − 1. `nextUint32()` gives the next word, and `nextDouble()` gives a float of 53 bits in [0, 1) from two words. |
|
|
46
|
+
| `Generator` | `numpy.random.Generator` | `new Generator(bitGenerator)`. `integers(low, high, size)`: `size` integers in [low, high), by Lemire's method. `random(size)`: `size` floats of 53 bits in [0, 1), from `nextDouble()`. `shuffle(values)`: shuffles in place, by masked rejection. `permutation(n)` or `permutation(values)`: a shuffled `0 … n − 1`, or a shuffled copy. `choice(a, size)`: `size` items of the array `a`, or of `0 … n − 1` when `a` is `n`, with replacement by `integers(0, n, size)`. `choice(a, size, { replace: false })`: `size` distinct items, by Floyd's algorithm, in a shuffled order. |
|
|
47
|
+
| `defaultRng` | `numpy.random.default_rng` | `defaultRng(seed)` is `new Generator(new PCG64(new SeedSequence(seed)))`. |
|
|
48
|
+
| `RandomState` | `numpy.random.RandomState`, the legacy generator of NumPy | `new RandomState(seed)` or `new RandomState(mt19937)`. `randomSample(size)` and `uniform(size)`: floats in [0, 1). `randint(low, high, size)`: integers in [low, high), by masked rejection. `choice(a, size)`, `choice(a, size, { p })` and `choice(a, size, { replace: false })`, where `a` is an array, or `n` for `0 … n − 1`. `permutation(n)` and `shuffle(values)`. scikit-learn's `check_random_state(seed)` gives this generator. |
|
|
49
|
+
|
|
50
|
+
`random_interval` (masked rejection) and `buffered_bounded_lemire_uint32` (Lemire's method) of NumPy's `distributions.c` are functions of the package over a `BitGenerator`. The package does not export them. `Generator` and `RandomState` both call the same `random_interval`, as NumPy calls one function with PCG64 or with MT19937.
|
|
51
|
+
|
|
52
|
+
## Seeds
|
|
53
|
+
|
|
54
|
+
A seed is a `number` or a `bigint`, from 0 up. A `bigint` gives the stream of a seed above 2^53, which a `number` cannot hold. NumPy takes an int of any size.
|
|
55
|
+
|
|
56
|
+
- A negative seed throws a `RangeError`, as NumPy raises a `ValueError` ("expected non-negative integer").
|
|
57
|
+
- A `number` that is not an integer, such as `1.5`, throws a `TypeError`, as NumPy raises a `TypeError`.
|
|
58
|
+
- A seed of `RandomState` or of `MT19937.legacy` above 2^32 − 1 throws a `RangeError`, as NumPy raises a `ValueError` ("Seed must be between 0 and 2\*\*32 - 1").
|
|
59
|
+
|
|
60
|
+
Both copies of the port today give the stream of `default_rng(4294967295)` for the seed −1 (`BigInt(-1) & MASK32` in `pool`), where NumPy raises a `ValueError`. The package fixes this, and a test proves the fix.
|
|
61
|
+
|
|
62
|
+
## Limits
|
|
63
|
+
|
|
64
|
+
The package ports the calls that its users make, and no more. A call outside its proven paths throws a `RangeError`, so it never gives numbers that NumPy does not give:
|
|
65
|
+
|
|
66
|
+
- `Generator.integers` and `RandomState.randint` take a range `high − low` of at most 2^32. NumPy uses 64-bit draws above that range. A range of 0 or less throws, as NumPy raises `ValueError: low >= high`. When `low` is 0, NumPy says `high <= 0`, and `Generator.integers` says it too. `RandomState.randint` throws for both today. `NumpyGenerator.integers` does not: `integers(5, 5, 1)` gives `[5]`, and a range above 2^32 gives numbers that NumPy does not give. Both throw in the package. `Generator.integers` of size 0 gives `[]` for all bounds, as NumPy does.
|
|
67
|
+
- `Generator.choice` with replacement takes a population of at most 2^32, the range of `integers`. Without replacement, it uses Floyd's algorithm only. A population above 10,000 with a size above a 50th of it throws, because NumPy shuffles the tail of a permutation there. A size above the population throws, as NumPy raises a `ValueError`. A population of 0 or less with a size above 0 throws, as NumPy raises a `ValueError`.
|
|
68
|
+
- `Generator.choice` has no `p`: its options type is `{ replace?: boolean }`. `RandomState.choice` takes `p` only with replacement: its options type does not let `p` and `replace: false` go together. TypeScript refuses these calls, so they do not throw.
|
|
69
|
+
- `RandomState.uniform` takes no `low` and no `high`: it gives the same floats as `randomSample`.
|
|
70
|
+
|
|
71
|
+
## What it does not do
|
|
72
|
+
|
|
73
|
+
| What | Trigger |
|
|
74
|
+
| ------------------------------------------------------------------------------------- | --------------------------------------------------- |
|
|
75
|
+
| other distributions, such as `normal` and `binomial` | a user draws from that distribution |
|
|
76
|
+
| other bit generators: `Philox`, `SFC64`, `PCG64DXSM`, `MT19937` from a `SeedSequence` | a user draws from that bit generator |
|
|
77
|
+
| 64-bit draws: ranges above 2^32, `nextUint64` | a user draws such a range |
|
|
78
|
+
| `Generator.choice` with `p`, or by the tail shuffle | a user calls it |
|
|
79
|
+
| a global generator, such as `numpy.random.seed` and `numpy.random.randint` | none: a user always holds its generator (NEP 19) |
|
|
80
|
+
| a `Generator` from a `SeedSequence` of many words, or `spawn` | a user seeds with a list of ints or spawns children |
|
|
81
|
+
|
|
82
|
+
## Proof
|
|
83
|
+
|
|
84
|
+
The package draws as NumPy 2.5.4 draws, bit for bit. `src/fixtures/generate.py` writes `src/fixtures/expected.json` with NumPy 2.5.4:
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
uv run --no-project --with numpy==2.5.4 python -I packages/random/src/fixtures/generate.py
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`src/random.golden.test.ts` reads it:
|
|
91
|
+
|
|
92
|
+
- the words of `SeedSequence.generateState`, `PCG64` and `MT19937.legacy`;
|
|
93
|
+
- the draws of `RandomState`, from a seed and from an `MT19937`: the `numpy` cases of mteb's golden fixture, and choices from an array with `p` and without replacement;
|
|
94
|
+
- the draws of `Generator`, from `defaultRng` and over `MT19937.legacy`: the `generator_cases` of mteb's golden fixture, the integers of `default_rng(31957)` and of `default_rng(2^40 + 5)` that both copies tested, the `bigint` seed 2^70 + 3, and the ranges 1, 2^32 − 1 and 2^32; the floats of `random`, also between 32-bit draws on one generator, which keep their buffered half; and the choices with replacement of `n` and of an array, for `n` from 1 to 2^32, as the Venn port draws them (`venn/heads/family.py:262`, `venn/outcomes.py:262`);
|
|
95
|
+
- the refusals: each call for which NumPy raises throws here, a `ValueError` as a `RangeError` with NumPy's message, and a `TypeError` as a `TypeError`, with each integer seed as a `number` and as a `bigint`. `RandomState.choice` refuses the weights `p` that NumPy refuses: a length other than `a`'s, a NaN, a negative weight, or a sum that is not 1 within the square root of the epsilon of the float type of `p` (`float32` for a `Float32Array`).
|
|
96
|
+
|
|
97
|
+
`src/proven-paths.test.ts` proves that the calls outside the proven paths throw a `RangeError`, where NumPy gives numbers: a range of 2^32 + 1, a choice with replacement of 2^32 + 1 items, and a choice of more than a 50th of a population above 10,000 (201 of 10,001).
|
|
98
|
+
|
|
99
|
+
## Who uses each part
|
|
100
|
+
|
|
101
|
+
| User | Part | Place |
|
|
102
|
+
| ----------------------------------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------- |
|
|
103
|
+
| relevance-filtering eval, topic bootstrap | `Generator.integers`, through the `randomIndex` of statistics | `packages/information-filtering/relevance-filtering/eval/bootstrap.ts:38`, `:43` |
|
|
104
|
+
| relevance-filtering eval, article weights | `Generator.integers` | `packages/information-filtering/relevance-filtering/eval/bootstrap.ts:59`, `:67` |
|
|
105
|
+
| relevance-filtering eval, random labels | `Generator.integers` | `packages/information-filtering/relevance-filtering/eval/daily.ts:280` |
|
|
106
|
+
|
|
107
|
+
`@zukhruf/statistics` does not depend on this package. Its `IidBootstrap` takes a `randomIndex` function, and each user gives `(n) => generator.integers(0, n, 1)[0]`.
|
|
108
|
+
|
|
109
|
+
## Documentation
|
|
110
|
+
|
|
111
|
+
**Recipes**
|
|
112
|
+
|
|
113
|
+
- [Give a bootstrap the draws of a NumPy program](./docs/recipes/bootstrap-with-the-draws-of-numpy.md)
|
|
114
|
+
|
|
115
|
+
**Decisions**
|
|
116
|
+
|
|
117
|
+
- [ADR 0001: one package for the generators of numpy.random, reached through a workspace path](docs/adr/0001-one-package-for-the-generators-of-numpy-random.md)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bit-generator.d.ts","sourceRoot":"","sources":["../../src/bit-generator/bit-generator.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,YAAY;IAE3B,UAAU,IAAI,MAAM,CAAC;IAGrB,UAAU,IAAI,MAAM,CAAC;CACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bit-generator.js","sourceRoot":"","sources":["../../src/bit-generator/bit-generator.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { BitGenerator } from './bit-generator.ts';
|
|
2
|
+
export declare class MT19937 implements BitGenerator {
|
|
3
|
+
#private;
|
|
4
|
+
private constructor();
|
|
5
|
+
static legacy(seed: number | bigint): MT19937;
|
|
6
|
+
nextUint32(): number;
|
|
7
|
+
nextDouble(): number;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=mt19937.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mt19937.d.ts","sourceRoot":"","sources":["../../src/bit-generator/mt19937.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AASvD,qBAAa,OAAQ,YAAW,YAAY;;IAI1C,OAAO,eAON;IAID,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAO5C;IAED,UAAU,IAAI,MAAM,CAQnB;IAGD,UAAU,IAAI,MAAM,CAInB;CAcF"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const N = 624;
|
|
2
|
+
const M = 397;
|
|
3
|
+
const MATRIX_A = 0x9908_b0df;
|
|
4
|
+
const UPPER_MASK = 0x8000_0000;
|
|
5
|
+
const LOWER_MASK = 0x7fff_ffff;
|
|
6
|
+
// numpy.random.MT19937, the Mersenne Twister of RandomState.
|
|
7
|
+
export class MT19937 {
|
|
8
|
+
#key = new Uint32Array(N);
|
|
9
|
+
#pos = N;
|
|
10
|
+
constructor(seed) {
|
|
11
|
+
let value = seed;
|
|
12
|
+
for (let pos = 0; pos < N; pos++) {
|
|
13
|
+
this.#key[pos] = value;
|
|
14
|
+
value =
|
|
15
|
+
(Math.imul(1_812_433_253, value ^ (value >>> 30)) + pos + 1) >>> 0;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// MT19937._legacy_seeding(seed), as RandomState(seed) seeds it:
|
|
19
|
+
// init_genrand of a seed from 0 to 2^32 - 1.
|
|
20
|
+
static legacy(seed) {
|
|
21
|
+
if (typeof seed === 'number' && !Number.isInteger(seed))
|
|
22
|
+
throw new TypeError(`A seed is an integer, not ${seed}`);
|
|
23
|
+
const value = BigInt(seed);
|
|
24
|
+
if (value < 0n || value > 0xffffffffn)
|
|
25
|
+
throw new RangeError('Seed must be between 0 and 2**32 - 1');
|
|
26
|
+
return new MT19937(Number(value));
|
|
27
|
+
}
|
|
28
|
+
nextUint32() {
|
|
29
|
+
if (this.#pos === N)
|
|
30
|
+
this.#twist();
|
|
31
|
+
let y = this.#key[this.#pos++] ?? 0;
|
|
32
|
+
y ^= y >>> 11;
|
|
33
|
+
y ^= (y << 7) & 0x9d2c_5680;
|
|
34
|
+
y ^= (y << 15) & 0xefc6_0000;
|
|
35
|
+
y ^= y >>> 18;
|
|
36
|
+
return y >>> 0;
|
|
37
|
+
}
|
|
38
|
+
// mt19937_next_double: a float of 53 bits in [0, 1) from two words.
|
|
39
|
+
nextDouble() {
|
|
40
|
+
const a = this.nextUint32() >>> 5;
|
|
41
|
+
const b = this.nextUint32() >>> 6;
|
|
42
|
+
return (a * 67_108_864 + b) / 9_007_199_254_740_992;
|
|
43
|
+
}
|
|
44
|
+
#twist() {
|
|
45
|
+
const key = this.#key;
|
|
46
|
+
const twist = (i, next, source) => {
|
|
47
|
+
const y = ((key[i] ?? 0) & UPPER_MASK) | ((key[next] ?? 0) & LOWER_MASK);
|
|
48
|
+
key[i] = (key[source] ?? 0) ^ (y >>> 1) ^ (y & 1 ? MATRIX_A : 0);
|
|
49
|
+
};
|
|
50
|
+
let i = 0;
|
|
51
|
+
for (; i < N - M; i++)
|
|
52
|
+
twist(i, i + 1, i + M);
|
|
53
|
+
for (; i < N - 1; i++)
|
|
54
|
+
twist(i, i + 1, i + (M - N));
|
|
55
|
+
twist(N - 1, 0, M - 1);
|
|
56
|
+
this.#pos = 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=mt19937.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mt19937.js","sourceRoot":"","sources":["../../src/bit-generator/mt19937.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,GAAG,GAAG,CAAC;AACd,MAAM,CAAC,GAAG,GAAG,CAAC;AACd,MAAM,QAAQ,GAAG,WAAW,CAAC;AAC7B,MAAM,UAAU,GAAG,WAAW,CAAC;AAC/B,MAAM,UAAU,GAAG,WAAW,CAAC;AAE/B,6DAA6D;AAC7D,MAAM,OAAO,OAAO;IACT,IAAI,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;IACnC,IAAI,GAAG,CAAC,CAAC;IAET,YAAoB,IAAY;QAC9B,IAAI,KAAK,GAAG,IAAI,CAAC;QACjB,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC;YACjC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;YACvB,KAAK;gBACH,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,GAAG,CAAC,KAAK,KAAK,EAAE,CAAC,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M;QACJ,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,MAAM,KAAK,GAAG,CAAC,CAAS,EAAE,IAAY,EAAE,MAAc,EAAE,EAAE;YACxD,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC;YACzE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACnE,CAAC,CAAC;QACF,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAAE,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAC9C,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAAE,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACpD,KAAK,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SeedSequence } from '../seed/seed-sequence.ts';
|
|
2
|
+
import type { BitGenerator } from './bit-generator.ts';
|
|
3
|
+
export declare class PCG64 implements BitGenerator {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(seed: number | bigint | SeedSequence);
|
|
6
|
+
nextUint32(): number;
|
|
7
|
+
nextDouble(): number;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=pcg64.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pcg64.d.ts","sourceRoot":"","sources":["../../src/bit-generator/pcg64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAQvD,qBAAa,KAAM,YAAW,YAAY;;IAOxC,YAAY,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,YAAY,EAgB/C;IAED,UAAU,IAAI,MAAM,CAMnB;IAID,UAAU,IAAI,MAAM,CAEnB;CAaF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { SeedSequence } from '../seed/seed-sequence.js';
|
|
2
|
+
const MASK32 = 0xffffffffn;
|
|
3
|
+
const MASK64 = (1n << 64n) - 1n;
|
|
4
|
+
const MASK128 = (1n << 128n) - 1n;
|
|
5
|
+
const MULTIPLIER = 0x2360ed051fc65da44385df649fccf645n;
|
|
6
|
+
// numpy.random.PCG64: a 128-bit LCG with the XSL-RR output of 64 bits.
|
|
7
|
+
export class PCG64 {
|
|
8
|
+
#state = 0n;
|
|
9
|
+
#increment;
|
|
10
|
+
// The high half of the last 64-bit output, which the next 32-bit call
|
|
11
|
+
// takes, as NumPy keeps it in the bit generator.
|
|
12
|
+
#halves = [];
|
|
13
|
+
constructor(seed) {
|
|
14
|
+
const sequence = seed instanceof SeedSequence ? seed : new SeedSequence(seed);
|
|
15
|
+
// generate_state(4, np.uint64): 8 words of 32 bits, read as 4 words of
|
|
16
|
+
// 64 bits, the low word first.
|
|
17
|
+
const words = sequence.generateState(8);
|
|
18
|
+
const [s0, s1, i0, i1] = [0, 2, 4, 6].map((index) => (BigInt(words[index + 1] ?? 0) << 32n) | BigInt(words[index] ?? 0));
|
|
19
|
+
// pcg_setseq_128_srandom_r.
|
|
20
|
+
this.#increment =
|
|
21
|
+
(((((i0 ?? 0n) << 64n) | (i1 ?? 0n)) << 1n) | 1n) & MASK128;
|
|
22
|
+
this.#step();
|
|
23
|
+
this.#state = (this.#state + (((s0 ?? 0n) << 64n) | (s1 ?? 0n))) & MASK128;
|
|
24
|
+
this.#step();
|
|
25
|
+
}
|
|
26
|
+
nextUint32() {
|
|
27
|
+
const half = this.#halves.pop();
|
|
28
|
+
if (half !== undefined)
|
|
29
|
+
return half;
|
|
30
|
+
const next = this.#next64();
|
|
31
|
+
this.#halves.push(Number(next >> 32n));
|
|
32
|
+
return Number(next & MASK32);
|
|
33
|
+
}
|
|
34
|
+
// pcg64_double: uint64_to_double of a new 64-bit output. It does not take
|
|
35
|
+
// the buffered half, so the next 32-bit call still takes it.
|
|
36
|
+
nextDouble() {
|
|
37
|
+
return Number(this.#next64() >> 11n) * 2 ** -53;
|
|
38
|
+
}
|
|
39
|
+
#next64() {
|
|
40
|
+
this.#step();
|
|
41
|
+
const high = this.#state >> 64n;
|
|
42
|
+
const value = high ^ (this.#state & MASK64);
|
|
43
|
+
const rotation = high >> 58n;
|
|
44
|
+
return ((value >> rotation) | (value << ((64n - rotation) & 63n))) & MASK64;
|
|
45
|
+
}
|
|
46
|
+
#step() {
|
|
47
|
+
this.#state = (this.#state * MULTIPLIER + this.#increment) & MASK128;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=pcg64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pcg64.js","sourceRoot":"","sources":["../../src/bit-generator/pcg64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAGxD,MAAM,MAAM,GAAG,WAAY,CAAC;AAC5B,MAAM,MAAM,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;AAChC,MAAM,OAAO,GAAG,CAAC,EAAE,IAAI,IAAI,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AAY,EAAE,MAAM,mCAAmC,CAAC;AAMtE,wBAAgB,aAAa,CAAC,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAYvE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const MASK32 = 0xffffffffn;
|
|
2
|
+
// buffered_bounded_lemire_uint32 of distributions.c: an integer in
|
|
3
|
+
// [0, range] by Lemire's method, for a range from 1 to 2^32 - 1.
|
|
4
|
+
export function boundedLemire(bits, range) {
|
|
5
|
+
const exclusive = BigInt(range) + 1n;
|
|
6
|
+
let m = BigInt(bits.nextUint32()) * exclusive;
|
|
7
|
+
let leftover = m & MASK32;
|
|
8
|
+
if (leftover < exclusive) {
|
|
9
|
+
const threshold = (MASK32 - BigInt(range)) % exclusive;
|
|
10
|
+
while (leftover < threshold) {
|
|
11
|
+
m = BigInt(bits.nextUint32()) * exclusive;
|
|
12
|
+
leftover = m & MASK32;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return Number(m >> 32n);
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=bounded-lemire.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounded-lemire.js","sourceRoot":"","sources":["../../src/generator/bounded-lemire.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,GAAG,WAAY,CAAC;AAE5B,mEAAmE;AACnE,iEAAiE;AACjE,MAAM,UAAU,aAAa,CAAC,IAAkB,EAAE,KAAa;IAC7D,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IACrC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,GAAG,SAAS,CAAC;IAC9C,IAAI,QAAQ,GAAG,CAAC,GAAG,MAAM,CAAC;IAC1B,IAAI,QAAQ,GAAG,SAAS,EAAE,CAAC;QACzB,MAAM,SAAS,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,SAAS,CAAC;QACvD,OAAO,QAAQ,GAAG,SAAS,EAAE,CAAC;YAC5B,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,GAAG,SAAS,CAAC;YAC1C,QAAQ,GAAG,CAAC,GAAG,MAAM,CAAC;QACxB,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-rng.d.ts","sourceRoot":"","sources":["../../src/generator/default-rng.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAI3C,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAE3D"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PCG64 } from '../bit-generator/pcg64.js';
|
|
2
|
+
import { SeedSequence } from '../seed/seed-sequence.js';
|
|
3
|
+
import { Generator } from './generator.js';
|
|
4
|
+
// numpy.random.default_rng(seed): a Generator over PCG64, seeded by a
|
|
5
|
+
// SeedSequence of the seed.
|
|
6
|
+
export function defaultRng(seed) {
|
|
7
|
+
return new Generator(new PCG64(new SeedSequence(seed)));
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=default-rng.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-rng.js","sourceRoot":"","sources":["../../src/generator/default-rng.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,sEAAsE;AACtE,4BAA4B;AAC5B,MAAM,UAAU,UAAU,CAAC,IAAqB;IAC9C,OAAO,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BitGenerator } from '../bit-generator/bit-generator.ts';
|
|
2
|
+
type ChoiceOptions = {
|
|
3
|
+
readonly replace?: boolean;
|
|
4
|
+
};
|
|
5
|
+
export declare class Generator {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(bitGenerator: BitGenerator);
|
|
8
|
+
integers(low: number, high: number, size: number): number[];
|
|
9
|
+
random(size: number): number[];
|
|
10
|
+
shuffle(values: unknown[]): void;
|
|
11
|
+
permutation(n: number): number[];
|
|
12
|
+
permutation<T>(values: readonly T[]): T[];
|
|
13
|
+
choice(a: number, size: number, options?: ChoiceOptions): number[];
|
|
14
|
+
choice<T>(a: readonly T[], size: number, options?: ChoiceOptions): T[];
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../src/generator/generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAMtE,KAAK,aAAa,GAAG;IAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAIpD,qBAAa,SAAS;;IAGpB,YAAY,YAAY,EAAE,YAAY,EAErC;IAMD,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAc1D;IAID,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAE7B;IAID,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAK/B;IAID,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACjC,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;IAa1C,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,EAAE,CAAC;IACnE,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,CAAC,EAAE,CAAC;CAgExE"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { boundedLemire } from './bounded-lemire.js';
|
|
2
|
+
import { randomInterval } from './random-interval.js';
|
|
3
|
+
// numpy.random.Generator: the draws of _generator.pyx over the bits of one
|
|
4
|
+
// bit generator (Bridge). Each call takes the next bits of the stream.
|
|
5
|
+
export class Generator {
|
|
6
|
+
#bits;
|
|
7
|
+
constructor(bitGenerator) {
|
|
8
|
+
this.#bits = bitGenerator;
|
|
9
|
+
}
|
|
10
|
+
// integers(low, high, size): size integers in [low, high), for a range
|
|
11
|
+
// high - low from 1 to 2^32. NumPy draws 64 bits above 2^32, a path this
|
|
12
|
+
// port does not prove. A size of 0 draws nothing and checks no bounds, as
|
|
13
|
+
// _rand_int64 returns before its checks.
|
|
14
|
+
integers(low, high, size) {
|
|
15
|
+
if (size === 0)
|
|
16
|
+
return [];
|
|
17
|
+
const range = high - low;
|
|
18
|
+
// format_bounds_error: NumPy names high alone when low is 0.
|
|
19
|
+
if (!(range > 0))
|
|
20
|
+
throw new RangeError(low === 0 ? 'high <= 0' : 'low >= high');
|
|
21
|
+
if (range > 2 ** 32)
|
|
22
|
+
throw new RangeError(`This port draws ranges of at most 2^32, not ${range}`);
|
|
23
|
+
return Array.from({ length: size }, () => low + this.#boundedUint64(range - 1));
|
|
24
|
+
}
|
|
25
|
+
// random(size): size floats of 53 bits in [0, 1), each the next_double of
|
|
26
|
+
// the bit generator (random_standard_uniform_fill).
|
|
27
|
+
random(size) {
|
|
28
|
+
return Array.from({ length: size }, () => this.#bits.nextDouble());
|
|
29
|
+
}
|
|
30
|
+
// shuffle(x) of a list or a 1-D array, in place: from the last index down,
|
|
31
|
+
// each item swaps with the item at random_interval(i).
|
|
32
|
+
shuffle(values) {
|
|
33
|
+
for (let i = values.length - 1; i >= 1; i--) {
|
|
34
|
+
const j = randomInterval(this.#bits, i);
|
|
35
|
+
[values[i], values[j]] = [values[j], values[i]];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
permutation(x) {
|
|
39
|
+
const values = typeof x === 'number'
|
|
40
|
+
? Array.from({ length: x }, (_, index) => index)
|
|
41
|
+
: [...x];
|
|
42
|
+
this.shuffle(values);
|
|
43
|
+
return values;
|
|
44
|
+
}
|
|
45
|
+
choice(a, size, { replace = true } = {}) {
|
|
46
|
+
const popSize = typeof a === 'number' ? a : a.length;
|
|
47
|
+
if (popSize <= 0 && size !== 0)
|
|
48
|
+
throw new RangeError(typeof a === 'number'
|
|
49
|
+
? 'a must be a positive integer unless no samples are taken'
|
|
50
|
+
: 'a cannot be empty unless no samples are taken');
|
|
51
|
+
const indices = replace
|
|
52
|
+
? this.integers(0, popSize, size)
|
|
53
|
+
: this.#floyd(popSize, size);
|
|
54
|
+
return typeof a === 'number'
|
|
55
|
+
? indices
|
|
56
|
+
: indices.map((index) => {
|
|
57
|
+
const item = a[index];
|
|
58
|
+
if (item === undefined)
|
|
59
|
+
throw new RangeError(`No item at ${index}`);
|
|
60
|
+
return item;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
// Without replacement: Floyd's algorithm draws size distinct indices, and
|
|
64
|
+
// _shuffle_int shuffles them. NumPy shuffles the tail of a permutation for
|
|
65
|
+
// a population above 10,000 and a size above a 50th of it, a path this
|
|
66
|
+
// port does not prove.
|
|
67
|
+
#floyd(popSize, size) {
|
|
68
|
+
if (size > popSize)
|
|
69
|
+
throw new RangeError('Cannot take a larger sample than population when replace is False');
|
|
70
|
+
if (popSize > 10_000 && size > Math.floor(popSize / 50))
|
|
71
|
+
throw new RangeError("This port chooses without replacement by Floyd's algorithm only");
|
|
72
|
+
const drawn = new Set();
|
|
73
|
+
const indices = [];
|
|
74
|
+
for (let j = popSize - size; j < popSize; j++) {
|
|
75
|
+
const value = this.#boundedUint64(j);
|
|
76
|
+
const index = drawn.has(value) ? j : value;
|
|
77
|
+
drawn.add(index);
|
|
78
|
+
indices.push(index);
|
|
79
|
+
}
|
|
80
|
+
this.#shuffleInt(indices);
|
|
81
|
+
return indices;
|
|
82
|
+
}
|
|
83
|
+
// _shuffle_int: from the last index down, each item swaps with the item at
|
|
84
|
+
// random_bounded_uint64(0, i).
|
|
85
|
+
#shuffleInt(values) {
|
|
86
|
+
for (let i = values.length - 1; i >= 1; i--) {
|
|
87
|
+
const j = this.#boundedUint64(i);
|
|
88
|
+
[values[i], values[j]] = [values[j], values[i]];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// random_bounded_uint64(off=0, rng) for rng below 2^32: no draw for rng 0,
|
|
92
|
+
// else Lemire's method.
|
|
93
|
+
#boundedUint64(rng) {
|
|
94
|
+
return rng === 0 ? 0 : boundedLemire(this.#bits, rng);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../../src/generator/generator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAMtD,2EAA2E;AAC3E,uEAAuE;AACvE,MAAM,OAAO,SAAS;IACX,KAAK,CAAe;IAE7B,YAAY,YAA0B;QACpC,IAAI,CAAC,KAAK,GAAG,YAAY,CAAC;IAC5B,CAAC;IAED,uEAAuE;IACvE,yEAAyE;IACzE,0EAA0E;IAC1E,yCAAyC;IACzC,QAAQ,CAAC,GAAW,EAAE,IAAY,EAAE,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QAClD,CAAC;IACH,CAAC;IAED,2EAA2E;IAC3E,wBAAwB;IACxB,cAAc,CAAC,GAAW;QACxB,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;IACxD,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-interval.d.ts","sourceRoot":"","sources":["../../src/generator/random-interval.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAKtE,wBAAgB,cAAc,CAAC,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAOtE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// random_interval(max) of distributions.c: an integer in [0, max] by masked
|
|
2
|
+
// rejection on 32-bit draws, for a max below 2^32. The shuffles of Generator
|
|
3
|
+
// and RandomState and the randint of RandomState draw with it.
|
|
4
|
+
export function randomInterval(bits, max) {
|
|
5
|
+
if (max === 0)
|
|
6
|
+
return 0;
|
|
7
|
+
const mask = max >= 0x8000_0000 ? 0xffff_ffff : 2 ** (32 - Math.clz32(max)) - 1;
|
|
8
|
+
let value = (bits.nextUint32() & mask) >>> 0;
|
|
9
|
+
while (value > max)
|
|
10
|
+
value = (bits.nextUint32() & mask) >>> 0;
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=random-interval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-interval.js","sourceRoot":"","sources":["../../src/generator/random-interval.ts"],"names":[],"mappings":"AAEA,4EAA4E;AAC5E,6EAA6E;AAC7E,+DAA+D;AAC/D,MAAM,UAAU,cAAc,CAAC,IAAkB,EAAE,GAAW;IAC5D,IAAI,GAAG,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IACxB,MAAM,IAAI,GACR,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IACrE,IAAI,KAAK,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7C,OAAO,KAAK,GAAG,GAAG;QAAE,KAAK,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7D,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { MT19937 } from '../bit-generator/mt19937.ts';
|
|
2
|
+
type ChoiceOptions = {
|
|
3
|
+
readonly p: ArrayLike<number>;
|
|
4
|
+
readonly replace?: never;
|
|
5
|
+
} | {
|
|
6
|
+
readonly replace: false;
|
|
7
|
+
readonly p?: never;
|
|
8
|
+
};
|
|
9
|
+
export declare class RandomState {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(seed: number | bigint | MT19937);
|
|
12
|
+
randomSample(size: number): number[];
|
|
13
|
+
uniform(size: number): number[];
|
|
14
|
+
randint(low: number, high: number, size: number): number[];
|
|
15
|
+
choice(a: number, size: number, options?: ChoiceOptions): number[];
|
|
16
|
+
choice<T>(a: readonly T[], size: number, options?: ChoiceOptions): T[];
|
|
17
|
+
permutation(n: number): number[];
|
|
18
|
+
shuffle(values: unknown[]): void;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=random-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-state.d.ts","sourceRoot":"","sources":["../../src/generator/random-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAStD,KAAK,aAAa,GACd;IAAE,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,KAAK,CAAA;CAAE,GAC3D;IAAE,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAIpD,qBAAa,WAAW;;IAGtB,YAAY,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,EAE1C;IAGD,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAEnC;IAID,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAE9B;IAKD,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAWzD;IAKD,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,EAAE,CAAC;IACnE,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,CAAC,EAAE,CAAC;IAwBvE,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAI/B;IAID,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAK/B;CAqEF"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { MT19937 } from '../bit-generator/mt19937.js';
|
|
2
|
+
import { randomInterval } from './random-interval.js';
|
|
3
|
+
// np.finfo(np.float32).eps.
|
|
4
|
+
const FLOAT32_EPSILON = 2 ** -23;
|
|
5
|
+
// numpy.random.RandomState, the legacy generator: the draws of mtrand.pyx
|
|
6
|
+
// over MT19937, whose stream NumPy keeps the same in each version (NEP 19).
|
|
7
|
+
export class RandomState {
|
|
8
|
+
#bits;
|
|
9
|
+
constructor(seed) {
|
|
10
|
+
this.#bits = seed instanceof MT19937 ? seed : MT19937.legacy(seed);
|
|
11
|
+
}
|
|
12
|
+
// random_sample(size): size floats in [0, 1) of 53 bits.
|
|
13
|
+
randomSample(size) {
|
|
14
|
+
return Array.from({ length: size }, () => this.#bits.nextDouble());
|
|
15
|
+
}
|
|
16
|
+
// uniform(size=size), with low 0 and high 1: low + (high - low) *
|
|
17
|
+
// random_sample(), which is random_sample().
|
|
18
|
+
uniform(size) {
|
|
19
|
+
return this.randomSample(size);
|
|
20
|
+
}
|
|
21
|
+
// randint(low, high, size) of int64 for a range high - low from 1 to 2^32:
|
|
22
|
+
// masked rejection on 32-bit draws (random_bounded_uint64_fill with
|
|
23
|
+
// use_masked), which is random_interval.
|
|
24
|
+
randint(low, high, size) {
|
|
25
|
+
const range = high - low;
|
|
26
|
+
if (!(range > 0))
|
|
27
|
+
throw new RangeError('low >= high');
|
|
28
|
+
if (range > 2 ** 32)
|
|
29
|
+
throw new RangeError(`This port draws ranges of at most 2^32, not ${range}`);
|
|
30
|
+
return Array.from({ length: size }, () => low + randomInterval(this.#bits, range - 1));
|
|
31
|
+
}
|
|
32
|
+
choice(a, size, options) {
|
|
33
|
+
const popSize = typeof a === 'number' ? a : a.length;
|
|
34
|
+
if (popSize <= 0 && size !== 0)
|
|
35
|
+
throw new RangeError(typeof a === 'number'
|
|
36
|
+
? 'a must be greater than 0 unless no samples are taken'
|
|
37
|
+
: "'a' cannot be empty unless no samples are taken");
|
|
38
|
+
const indices = this.#choiceIndices(popSize, size, options);
|
|
39
|
+
return typeof a === 'number'
|
|
40
|
+
? indices
|
|
41
|
+
: indices.map((index) => {
|
|
42
|
+
const item = a[index];
|
|
43
|
+
if (item === undefined)
|
|
44
|
+
throw new RangeError(`No item at ${index}`);
|
|
45
|
+
return item;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
// permutation(n): a shuffle of arange(n).
|
|
49
|
+
permutation(n) {
|
|
50
|
+
const values = Array.from({ length: n }, (_, index) => index);
|
|
51
|
+
this.shuffle(values);
|
|
52
|
+
return values;
|
|
53
|
+
}
|
|
54
|
+
// shuffle(x) in place: from the last index down, each item swaps with the
|
|
55
|
+
// item at random_interval(i).
|
|
56
|
+
shuffle(values) {
|
|
57
|
+
for (let i = values.length - 1; i >= 1; i--) {
|
|
58
|
+
const j = randomInterval(this.#bits, i);
|
|
59
|
+
[values[i], values[j]] = [values[j], values[i]];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
#choiceIndices(popSize, size, options) {
|
|
63
|
+
if (options?.replace === false) {
|
|
64
|
+
if (size > popSize)
|
|
65
|
+
throw new RangeError("Cannot take a larger sample than population when 'replace=False'");
|
|
66
|
+
return this.permutation(popSize).slice(0, size);
|
|
67
|
+
}
|
|
68
|
+
if (options?.p !== undefined)
|
|
69
|
+
return this.#weighted(options.p, popSize, size);
|
|
70
|
+
return this.randint(0, popSize, size);
|
|
71
|
+
}
|
|
72
|
+
// The cumulative sum of p in float64, divided by its last value, searched
|
|
73
|
+
// on the right for each random_sample. p holds no NaN and no negative
|
|
74
|
+
// value, and its Kahan sum is 1 within the square root of the epsilon of
|
|
75
|
+
// its float type, as NumPy checks.
|
|
76
|
+
#weighted(p, popSize, size) {
|
|
77
|
+
if (p.length !== popSize)
|
|
78
|
+
throw new RangeError("'a' and 'p' must have same size");
|
|
79
|
+
const weights = Array.from(p);
|
|
80
|
+
const sum = RandomState.#kahanSum(weights);
|
|
81
|
+
if (Number.isNaN(sum))
|
|
82
|
+
throw new RangeError('probabilities contain NaN');
|
|
83
|
+
if (weights.some((weight) => weight < 0))
|
|
84
|
+
throw new RangeError('probabilities are not non-negative');
|
|
85
|
+
const tolerance = Math.sqrt(p instanceof Float32Array ? FLOAT32_EPSILON : Number.EPSILON);
|
|
86
|
+
if (Math.abs(sum - 1) > tolerance)
|
|
87
|
+
throw new RangeError('probabilities do not sum to 1');
|
|
88
|
+
const cdf = new Float64Array(p.length);
|
|
89
|
+
let total = 0;
|
|
90
|
+
for (let index = 0; index < p.length; index++) {
|
|
91
|
+
total += p[index] ?? 0;
|
|
92
|
+
cdf[index] = total;
|
|
93
|
+
}
|
|
94
|
+
const last = cdf.at(-1) ?? 1;
|
|
95
|
+
for (let index = 0; index < cdf.length; index++)
|
|
96
|
+
cdf[index] = (cdf[index] ?? 0) / last;
|
|
97
|
+
return this.randomSample(size).map((value) => {
|
|
98
|
+
let low = 0;
|
|
99
|
+
let high = cdf.length;
|
|
100
|
+
while (low < high) {
|
|
101
|
+
const middle = (low + high) >>> 1;
|
|
102
|
+
if ((cdf[middle] ?? 0) <= value)
|
|
103
|
+
low = middle + 1;
|
|
104
|
+
else
|
|
105
|
+
high = middle;
|
|
106
|
+
}
|
|
107
|
+
return low;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
// kahan_sum of mtrand.pyx.
|
|
111
|
+
static #kahanSum(values) {
|
|
112
|
+
let sum = values[0] ?? 0;
|
|
113
|
+
let compensation = 0;
|
|
114
|
+
for (const value of values.slice(1)) {
|
|
115
|
+
const y = value - compensation;
|
|
116
|
+
const t = sum + y;
|
|
117
|
+
compensation = t - sum - y;
|
|
118
|
+
sum = t;
|
|
119
|
+
}
|
|
120
|
+
return sum;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=random-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-state.js","sourceRoot":"","sources":["../../src/generator/random-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,4BAA4B;AAC5B,MAAM,eAAe,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;AASjC,0EAA0E;AAC1E,4EAA4E;AAC5E,MAAM,OAAO,WAAW;IACb,KAAK,CAAU;IAExB,YAAY,IAA+B;QACzC,IAAI,CAAC,KAAK,GAAG,IAAI,YAAY,OAAO,CAAC,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,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACzB,IAAI,YAAY,GAAG,CAAC,CAAC;QACrB,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;YACpC,MAAM,CAAC,GAAG,KAAK,GAAG,YAAY,CAAC;YAC/B,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;YAClB,YAAY,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;YAC3B,GAAG,GAAG,CAAC,CAAC;QACV,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { BitGenerator } from './bit-generator/bit-generator.ts';
|
|
2
|
+
export { MT19937 } from './bit-generator/mt19937.ts';
|
|
3
|
+
export { PCG64 } from './bit-generator/pcg64.ts';
|
|
4
|
+
export { defaultRng } from './generator/default-rng.ts';
|
|
5
|
+
export { Generator } from './generator/generator.ts';
|
|
6
|
+
export { RandomState } from './generator/random-state.ts';
|
|
7
|
+
export { SeedSequence } from './seed/seed-sequence.ts';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { MT19937 } from './bit-generator/mt19937.js';
|
|
2
|
+
export { PCG64 } from './bit-generator/pcg64.js';
|
|
3
|
+
export { defaultRng } from './generator/default-rng.js';
|
|
4
|
+
export { Generator } from './generator/generator.js';
|
|
5
|
+
export { RandomState } from './generator/random-state.js';
|
|
6
|
+
export { SeedSequence } from './seed/seed-sequence.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-sequence.d.ts","sourceRoot":"","sources":["../../src/seed/seed-sequence.ts"],"names":[],"mappings":"AAWA,qBAAa,YAAY;;IAGvB,YAAY,IAAI,EAAE,MAAM,GAAG,MAAM,EAIhC;IAID,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAUzC;CAoDF"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const MASK32 = 0xffffffffn;
|
|
2
|
+
const POOL_SIZE = 4;
|
|
3
|
+
const INIT_A = 0x43b0_d7e5;
|
|
4
|
+
const MULT_A = 0x931e_8875;
|
|
5
|
+
const INIT_B = 0x8b51_f9dd;
|
|
6
|
+
const MULT_B = 0x58f3_8ded;
|
|
7
|
+
const MIX_MULT_L = 0xca01_f9dd;
|
|
8
|
+
const MIX_MULT_R = 0x4973_f715;
|
|
9
|
+
// numpy.random.SeedSequence(seed) without a spawn key: the seed, cut into
|
|
10
|
+
// words of 32 bits from the low end, is mixed into a pool of 4 words.
|
|
11
|
+
export class SeedSequence {
|
|
12
|
+
#pool;
|
|
13
|
+
constructor(seed) {
|
|
14
|
+
this.#pool = SeedSequence.#mixEntropy(SeedSequence.#entropy(SeedSequence.#seedValue(seed)));
|
|
15
|
+
}
|
|
16
|
+
// generate_state(nWords, dtype=np.uint32): nWords words from the pool, in
|
|
17
|
+
// turn.
|
|
18
|
+
generateState(nWords) {
|
|
19
|
+
const state = new Uint32Array(nWords);
|
|
20
|
+
let hashConst = INIT_B;
|
|
21
|
+
for (let index = 0; index < nWords; index++) {
|
|
22
|
+
let value = ((this.#pool[index % POOL_SIZE] ?? 0) ^ hashConst) >>> 0;
|
|
23
|
+
hashConst = Math.imul(hashConst, MULT_B) >>> 0;
|
|
24
|
+
value = Math.imul(value, hashConst) >>> 0;
|
|
25
|
+
state[index] = (value ^ (value >>> 16)) >>> 0;
|
|
26
|
+
}
|
|
27
|
+
return state;
|
|
28
|
+
}
|
|
29
|
+
// A seed as NumPy reads it: an int of any size, from 0 up.
|
|
30
|
+
static #seedValue(seed) {
|
|
31
|
+
if (typeof seed === 'number' && !Number.isInteger(seed))
|
|
32
|
+
throw new TypeError(`SeedSequence expects int or sequence of ints for entropy not ${seed}`);
|
|
33
|
+
const value = BigInt(seed);
|
|
34
|
+
if (value < 0n)
|
|
35
|
+
throw new RangeError('expected non-negative integer');
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
// _coerce_to_uint32_array: the words of 32 bits of the seed, the low word
|
|
39
|
+
// first; the seed 0 is one word.
|
|
40
|
+
static #entropy(seed) {
|
|
41
|
+
const words = [];
|
|
42
|
+
let rest = seed;
|
|
43
|
+
do {
|
|
44
|
+
words.push(Number(rest & MASK32));
|
|
45
|
+
rest >>= 32n;
|
|
46
|
+
} while (rest > 0n);
|
|
47
|
+
return words;
|
|
48
|
+
}
|
|
49
|
+
// mix_entropy: each word of the pool takes the hash of each other word,
|
|
50
|
+
// then each word of the entropy past the pool.
|
|
51
|
+
static #mixEntropy(entropy) {
|
|
52
|
+
let hashConst = INIT_A;
|
|
53
|
+
const hashmix = (value) => {
|
|
54
|
+
let mixed = (value ^ hashConst) >>> 0;
|
|
55
|
+
hashConst = Math.imul(hashConst, MULT_A) >>> 0;
|
|
56
|
+
mixed = Math.imul(mixed, hashConst) >>> 0;
|
|
57
|
+
return (mixed ^ (mixed >>> 16)) >>> 0;
|
|
58
|
+
};
|
|
59
|
+
const mix = (x, y) => {
|
|
60
|
+
const result = (Math.imul(MIX_MULT_L, x) - Math.imul(MIX_MULT_R, y)) >>> 0;
|
|
61
|
+
return (result ^ (result >>> 16)) >>> 0;
|
|
62
|
+
};
|
|
63
|
+
const pool = Array.from({ length: POOL_SIZE }, (_, index) => hashmix(entropy[index] ?? 0));
|
|
64
|
+
for (let source = 0; source < POOL_SIZE; source++)
|
|
65
|
+
for (let target = 0; target < POOL_SIZE; target++)
|
|
66
|
+
if (source !== target)
|
|
67
|
+
pool[target] = mix(pool[target] ?? 0, hashmix(pool[source] ?? 0));
|
|
68
|
+
for (let source = POOL_SIZE; source < entropy.length; source++)
|
|
69
|
+
for (let target = 0; target < POOL_SIZE; target++)
|
|
70
|
+
pool[target] = mix(pool[target] ?? 0, hashmix(entropy[source] ?? 0));
|
|
71
|
+
return pool;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=seed-sequence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-sequence.js","sourceRoot":"","sources":["../../src/seed/seed-sequence.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,WAAY,CAAC;AAC5B,MAAM,SAAS,GAAG,CAAC,CAAC;AACpB,MAAM,MAAM,GAAG,WAAW,CAAC;AAC3B,MAAM,MAAM,GAAG,WAAW,CAAC;AAC3B,MAAM,MAAM,GAAG,WAA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py.random in JavaScript, to the last bit: SeedSequence, PCG64, the legacy MT19937, Generator, default_rng and RandomState.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/JanuaryLabs/zukhruf",
|
|
9
|
+
"directory": "packages/random"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/JanuaryLabs/zukhruf/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/random#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"random",
|
|
17
|
+
"numpy",
|
|
18
|
+
"pcg64",
|
|
19
|
+
"mt19937",
|
|
20
|
+
"seed-sequence"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
"./package.json": "./package.json",
|
|
25
|
+
".": {
|
|
26
|
+
"@zukhruf/source": "./src/index.ts",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"NOTICE",
|
|
35
|
+
"!dist/spec/**",
|
|
36
|
+
"!**/*.tsbuildinfo",
|
|
37
|
+
"!**/*.test.*"
|
|
38
|
+
],
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@zukhruf/parameterized-test": "^0.8.276"
|
|
44
|
+
}
|
|
45
|
+
}
|