@ac-kit/math-random 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0] - 2026-08-26
4
+
5
+ ### Added
6
+
7
+ - Initial release.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # @ac-kit/math-random
2
+
3
+ Seeded pseudo-random number generators: give one a seed and it produces the same
4
+ sequence every time.
5
+
6
+ That is exactly what `Math.random()` cannot do. A simulation you want to replay,
7
+ a test that must fail the same way twice, a procedurally generated level that
8
+ looks identical on every machine — all need the sequence to be a function of the
9
+ seed.
10
+
11
+ ```ts
12
+ import { xorshift32 } from "@ac-kit/math-random";
13
+
14
+ const random = xorshift32(12345);
15
+ random(); // same value on every run, every machine
16
+ random();
17
+ ```
18
+
19
+ Each generator is a factory taking a seed and returning a `RandomFn` — a
20
+ zero-argument function yielding a float in `[0, 1)`, the same contract as
21
+ `Math.random`, so it drops into anything accepting an injectable random source,
22
+ such as `@ac-kit/algo`'s `shuffle`.
23
+
24
+ Available: `mulberry32`, `pcg32`, `sfc32`, `splitmix64`, `xorshift32`,
25
+ `xoshiro128p`, `xoshiro256p` — differing in state size, period and speed.
26
+
27
+ **None of these is cryptographically secure.** Their output is predictable from
28
+ a few samples by design. For keys, tokens or nonces use `@ac-kit/crypto-random`.
29
+
30
+ Portable: no Node.js built-ins, so it runs unchanged in browsers, Deno, Bun and
31
+ edge runtimes.
32
+
33
+ See the
34
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/math-random/)
35
+ for the full reference.
@@ -0,0 +1,8 @@
1
+ export * from "./mulberry32.js";
2
+ export * from "./pcg32.js";
3
+ export * from "./sfc32.js";
4
+ export * from "./splitmix64.js";
5
+ export * from "./xorshift32.js";
6
+ export * from "./xoshiro128p.js";
7
+ export * from "./xoshiro256p.js";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./mulberry32.js";
2
+ export * from "./pcg32.js";
3
+ export * from "./sfc32.js";
4
+ export * from "./splitmix64.js";
5
+ export * from "./xorshift32.js";
6
+ export * from "./xoshiro128p.js";
7
+ export * from "./xoshiro256p.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the Mulberry32
4
+ * algorithm.
5
+ *
6
+ * @param seed - The seed value to initialize the generator. It should be a
7
+ * 32-bit unsigned integer.
8
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
9
+ * when called.
10
+ */
11
+ export declare function mulberry32(seed: number): RandomFn;
12
+ //# sourceMappingURL=mulberry32.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mulberry32.d.ts","sourceRoot":"","sources":["../src/mulberry32.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAQjD"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Creates a seeded pseudo-random number generator function using the Mulberry32
3
+ * algorithm.
4
+ *
5
+ * @param seed - The seed value to initialize the generator. It should be a
6
+ * 32-bit unsigned integer.
7
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
8
+ * when called.
9
+ */
10
+ export function mulberry32(seed) {
11
+ let s = seed >>> 0;
12
+ return () => {
13
+ s = (s + 0x6d2b79f5) | 0;
14
+ let t = Math.imul(s ^ (s >>> 15), 1 | s);
15
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
16
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
17
+ };
18
+ }
19
+ //# sourceMappingURL=mulberry32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mulberry32.js","sourceRoot":"","sources":["../src/mulberry32.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACtC,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC;IACnB,OAAO,GAAG,EAAE;QACX,CAAC,GAAG,CAAC,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QACzB,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACzC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAC/C,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,UAAU,CAAC;IAC9C,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { type RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the PCG32
4
+ * algorithm (Permuted Congruential Generator, 32-bit output) by Melissa O'Neill
5
+ * (2014).
6
+ *
7
+ * Uses a 64-bit LCG state with a scrambled output function. Excellent
8
+ * statistical quality.
9
+ *
10
+ * The seed is accepted as a JS `number`; values above 2^53 may lose precision
11
+ * due to IEEE 754 double representation before the BigInt conversion.
12
+ *
13
+ * @param seed - The seed value to initialize the 64-bit internal state.
14
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
15
+ * when called.
16
+ */
17
+ export declare function pcg32(seed: number): RandomFn;
18
+ //# sourceMappingURL=pcg32.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pcg32.d.ts","sourceRoot":"","sources":["../src/pcg32.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE/D;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAW5C"}
package/dist/pcg32.js ADDED
@@ -0,0 +1,28 @@
1
+ import { MASK_64N, rotr32 } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the PCG32
4
+ * algorithm (Permuted Congruential Generator, 32-bit output) by Melissa O'Neill
5
+ * (2014).
6
+ *
7
+ * Uses a 64-bit LCG state with a scrambled output function. Excellent
8
+ * statistical quality.
9
+ *
10
+ * The seed is accepted as a JS `number`; values above 2^53 may lose precision
11
+ * due to IEEE 754 double representation before the BigInt conversion.
12
+ *
13
+ * @param seed - The seed value to initialize the 64-bit internal state.
14
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
15
+ * when called.
16
+ */
17
+ export function pcg32(seed) {
18
+ let state = BigInt(seed) & MASK_64N;
19
+ const multiplier = 6364136223846793005n;
20
+ const increment = 1442695040888963407n;
21
+ return () => {
22
+ state = (state * multiplier + increment) & MASK_64N;
23
+ const xorshifted = Number(((state >> 18n) ^ state) >> 27n) | 0;
24
+ const rot = Number(state >> 59n);
25
+ return rotr32(xorshifted, rot) / 4294967296;
26
+ };
27
+ }
28
+ //# sourceMappingURL=pcg32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pcg32.js","sourceRoot":"","sources":["../src/pcg32.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAiB,MAAM,cAAc,CAAC;AAE/D;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,KAAK,CAAC,IAAY;IACjC,IAAI,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;IACpC,MAAM,UAAU,GAAG,oBAAoB,CAAC;IACxC,MAAM,SAAS,GAAG,oBAAoB,CAAC;IAEvC,OAAO,GAAG,EAAE;QACX,KAAK,GAAG,CAAC,KAAK,GAAG,UAAU,GAAG,SAAS,CAAC,GAAG,QAAQ,CAAC;QACpD,MAAM,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QAC/D,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,CAAC;QACjC,OAAO,MAAM,CAAC,UAAU,EAAE,GAAG,CAAC,GAAG,UAAU,CAAC;IAC7C,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { type RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the SFC32
4
+ * algorithm (Small Fast Counting, 32-bit output) by Chris Doty-Humphrey.
5
+ *
6
+ * Uses 128-bit state (four 32-bit words) and an internal counter for guaranteed
7
+ * long periods. Passes PractRand at very high lengths.
8
+ *
9
+ * @param a - First seed word (32-bit unsigned integer).
10
+ * @param b - Second seed word (32-bit unsigned integer).
11
+ * @param c - Third seed word (32-bit unsigned integer).
12
+ * @param d - Counter word (32-bit unsigned integer); start at 0 or any value.
13
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
14
+ * when called.
15
+ */
16
+ export declare function sfc32(a: number, b: number, c: number, d: number): RandomFn;
17
+ //# sourceMappingURL=sfc32.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sfc32.d.ts","sourceRoot":"","sources":["../src/sfc32.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,QAAQ,CAe1E"}
package/dist/sfc32.js ADDED
@@ -0,0 +1,32 @@
1
+ import { rotl32 } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the SFC32
4
+ * algorithm (Small Fast Counting, 32-bit output) by Chris Doty-Humphrey.
5
+ *
6
+ * Uses 128-bit state (four 32-bit words) and an internal counter for guaranteed
7
+ * long periods. Passes PractRand at very high lengths.
8
+ *
9
+ * @param a - First seed word (32-bit unsigned integer).
10
+ * @param b - Second seed word (32-bit unsigned integer).
11
+ * @param c - Third seed word (32-bit unsigned integer).
12
+ * @param d - Counter word (32-bit unsigned integer); start at 0 or any value.
13
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
14
+ * when called.
15
+ */
16
+ export function sfc32(a, b, c, d) {
17
+ a = a >>> 0;
18
+ b = b >>> 0;
19
+ c = c >>> 0;
20
+ d = d >>> 0;
21
+ return () => {
22
+ const t = (a + b) | 0;
23
+ a = b ^ (b >>> 9);
24
+ b = (c + (c << 3)) | 0;
25
+ c = rotl32(c, 21);
26
+ d = (d + 1) | 0;
27
+ const result = (t + d) | 0;
28
+ c = (c + result) | 0;
29
+ return (result >>> 0) / 4294967296;
30
+ };
31
+ }
32
+ //# sourceMappingURL=sfc32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sfc32.js","sourceRoot":"","sources":["../src/sfc32.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAiB,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,KAAK,CAAC,CAAS,EAAE,CAAS,EAAE,CAAS,EAAE,CAAS;IAC/D,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACZ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACZ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACZ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACZ,OAAO,GAAG,EAAE;QACX,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;QAClB,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAClB,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;QAChB,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;QAC3B,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QACrB,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,GAAG,UAAU,CAAC;IACpC,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { type RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the SplitMix64
4
+ * algorithm by Guy Steele and Sebastiano Vigna. Single 64-bit state word,
5
+ * 64-bit output.
6
+ *
7
+ * Commonly used to expand a single integer seed into an initial state for
8
+ * multi-word generators such as xoshiro256+. Fast and passes BigCrush.
9
+ *
10
+ * The seed is accepted as a JS `number`; values above 2^53 may lose precision
11
+ * due to IEEE 754 double representation before the BigInt conversion.
12
+ *
13
+ * @param seed - The seed value to initialize the 64-bit internal state.
14
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
15
+ * when called.
16
+ */
17
+ export declare function splitmix64(seed: number): RandomFn;
18
+ //# sourceMappingURL=splitmix64.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"splitmix64.d.ts","sourceRoot":"","sources":["../src/splitmix64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAEvD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAUjD"}
@@ -0,0 +1,27 @@
1
+ import { MASK_64N } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the SplitMix64
4
+ * algorithm by Guy Steele and Sebastiano Vigna. Single 64-bit state word,
5
+ * 64-bit output.
6
+ *
7
+ * Commonly used to expand a single integer seed into an initial state for
8
+ * multi-word generators such as xoshiro256+. Fast and passes BigCrush.
9
+ *
10
+ * The seed is accepted as a JS `number`; values above 2^53 may lose precision
11
+ * due to IEEE 754 double representation before the BigInt conversion.
12
+ *
13
+ * @param seed - The seed value to initialize the 64-bit internal state.
14
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
15
+ * when called.
16
+ */
17
+ export function splitmix64(seed) {
18
+ let x = BigInt(seed) & MASK_64N;
19
+ return () => {
20
+ x = (x + 0x9e3779b97f4a7c15n) & MASK_64N;
21
+ let z = x;
22
+ z = ((z ^ (z >> 30n)) * 0xbf58476d1ce4e5b9n) & MASK_64N;
23
+ z = ((z ^ (z >> 27n)) * 0x94d049bb133111ebn) & MASK_64N;
24
+ return Number((z ^ (z >> 31n)) >> 11n) / 2 ** 53;
25
+ };
26
+ }
27
+ //# sourceMappingURL=splitmix64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"splitmix64.js","sourceRoot":"","sources":["../src/splitmix64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAC;AAEvD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACtC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;IAEhC,OAAO,GAAG,EAAE;QACX,CAAC,GAAG,CAAC,CAAC,GAAG,mBAAmB,CAAC,GAAG,QAAQ,CAAC;QACzC,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,mBAAmB,CAAC,GAAG,QAAQ,CAAC;QACxD,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,mBAAmB,CAAC,GAAG,QAAQ,CAAC;QACxD,OAAO,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IAClD,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the xorshift32
4
+ * algorithm by George Marsaglia (2003). 32-bit state, 32-bit output.
5
+ *
6
+ * Note: Fails some PractRand tests at large sample sizes, so prefer
7
+ * {@link sfc32}/{@link xoshiro128p} for statistical work; fine for
8
+ * non-adversarial deterministic fixtures, and simple enough to reproduce
9
+ * bit-for-bit in another language (C, Python, Rust, ...) when a fixture must
10
+ * match across an FFI or subprocess boundary.
11
+ *
12
+ * @param seed - The seed value to initialize the 32-bit state. Must be nonzero
13
+ * (an all-zero state never changes).
14
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
15
+ * when called.
16
+ */
17
+ export declare function xorshift32(seed: number): RandomFn;
18
+ //# sourceMappingURL=xorshift32.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xorshift32.d.ts","sourceRoot":"","sources":["../src/xorshift32.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CASjD"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Creates a seeded pseudo-random number generator function using the xorshift32
3
+ * algorithm by George Marsaglia (2003). 32-bit state, 32-bit output.
4
+ *
5
+ * Note: Fails some PractRand tests at large sample sizes, so prefer
6
+ * {@link sfc32}/{@link xoshiro128p} for statistical work; fine for
7
+ * non-adversarial deterministic fixtures, and simple enough to reproduce
8
+ * bit-for-bit in another language (C, Python, Rust, ...) when a fixture must
9
+ * match across an FFI or subprocess boundary.
10
+ *
11
+ * @param seed - The seed value to initialize the 32-bit state. Must be nonzero
12
+ * (an all-zero state never changes).
13
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
14
+ * when called.
15
+ */
16
+ export function xorshift32(seed) {
17
+ let state = seed >>> 0;
18
+ return () => {
19
+ state = (state ^ (state << 13)) >>> 0;
20
+ state = (state ^ (state >>> 17)) >>> 0;
21
+ state = (state ^ (state << 5)) >>> 0;
22
+ return state / 4294967296;
23
+ };
24
+ }
25
+ //# sourceMappingURL=xorshift32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xorshift32.js","sourceRoot":"","sources":["../src/xorshift32.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACtC,IAAI,KAAK,GAAG,IAAI,KAAK,CAAC,CAAC;IAEvB,OAAO,GAAG,EAAE;QACX,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;QACtC,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,KAAK,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;QACvC,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACrC,OAAO,KAAK,GAAG,UAAU,CAAC;IAC3B,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { type RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the
4
+ * xoshiro128+ algorithm by Blackman & Vigna (2018). 128-bit state, 32-bit
5
+ * output.
6
+ *
7
+ * Recommended for floating-point generation. Passes PractRand. Faster than the
8
+ * `**` variant.
9
+ *
10
+ * @param seed - The seed value used to derive the four 32-bit state words.
11
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
12
+ * when called.
13
+ */
14
+ export declare function xoshiro128p(seed: number): RandomFn;
15
+ //# sourceMappingURL=xoshiro128p.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xoshiro128p.d.ts","sourceRoot":"","sources":["../src/xoshiro128p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAiBlD"}
@@ -0,0 +1,31 @@
1
+ import { rotl32 } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the
4
+ * xoshiro128+ algorithm by Blackman & Vigna (2018). 128-bit state, 32-bit
5
+ * output.
6
+ *
7
+ * Recommended for floating-point generation. Passes PractRand. Faster than the
8
+ * `**` variant.
9
+ *
10
+ * @param seed - The seed value used to derive the four 32-bit state words.
11
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
12
+ * when called.
13
+ */
14
+ export function xoshiro128p(seed) {
15
+ let s0 = seed >>> 0;
16
+ let s1 = (seed ^ 0x6d2b79f5) >>> 0;
17
+ let s2 = (seed ^ 0x9e3779b9) >>> 0;
18
+ let s3 = (seed ^ 0x7f4a7c15) >>> 0;
19
+ return () => {
20
+ const result = (s0 + s3) >>> 0;
21
+ const t = s1 << 9;
22
+ s2 ^= s0;
23
+ s3 ^= s1;
24
+ s1 ^= s2;
25
+ s0 ^= s3;
26
+ s2 ^= t;
27
+ s3 = rotl32(s3, 11);
28
+ return result / 4294967296;
29
+ };
30
+ }
31
+ //# sourceMappingURL=xoshiro128p.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xoshiro128p.js","sourceRoot":"","sources":["../src/xoshiro128p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAiB,MAAM,cAAc,CAAC;AAErD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACvC,IAAI,EAAE,GAAG,IAAI,KAAK,CAAC,CAAC;IACpB,IAAI,EAAE,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;IACnC,IAAI,EAAE,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;IACnC,IAAI,EAAE,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;IAEnC,OAAO,GAAG,EAAE;QACX,MAAM,MAAM,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;QAC/B,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAClB,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,CAAC,CAAC;QACR,EAAE,GAAG,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QACpB,OAAO,MAAM,GAAG,UAAU,CAAC;IAC5B,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { type RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the
4
+ * xoshiro256+ algorithm by Blackman & Vigna (2018). 256-bit state, 64-bit
5
+ * output mapped to [0, 1).
6
+ *
7
+ * Recommended for floating-point generation. The top 53 bits of the 64-bit
8
+ * output are used to preserve IEEE 754 double precision.
9
+ *
10
+ * The seed is accepted as a JS `number`; values above 2^53 may lose precision
11
+ * due to IEEE 754 double representation before the BigInt conversion.
12
+ *
13
+ * @param seed - The seed value used to derive the four 64-bit state words.
14
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
15
+ * when called.
16
+ */
17
+ export declare function xoshiro256p(seed: number): RandomFn;
18
+ //# sourceMappingURL=xoshiro256p.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xoshiro256p.d.ts","sourceRoot":"","sources":["../src/xoshiro256p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE/D;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAkBlD"}
@@ -0,0 +1,35 @@
1
+ import { MASK_64N, rotl64 } from "@ac-kit/core";
2
+ /**
3
+ * Creates a seeded pseudo-random number generator function using the
4
+ * xoshiro256+ algorithm by Blackman & Vigna (2018). 256-bit state, 64-bit
5
+ * output mapped to [0, 1).
6
+ *
7
+ * Recommended for floating-point generation. The top 53 bits of the 64-bit
8
+ * output are used to preserve IEEE 754 double precision.
9
+ *
10
+ * The seed is accepted as a JS `number`; values above 2^53 may lose precision
11
+ * due to IEEE 754 double representation before the BigInt conversion.
12
+ *
13
+ * @param seed - The seed value used to derive the four 64-bit state words.
14
+ * @returns A function that generates pseudo-random numbers in the range [0, 1)
15
+ * when called.
16
+ */
17
+ export function xoshiro256p(seed) {
18
+ const seedBig = BigInt(seed);
19
+ let s0 = seedBig & MASK_64N;
20
+ let s1 = (seedBig ^ 0x6d2b79f5n) & MASK_64N;
21
+ let s2 = (seedBig ^ 0x9e3779b9n) & MASK_64N;
22
+ let s3 = (seedBig ^ 0x7f4a7c15n) & MASK_64N;
23
+ return () => {
24
+ const result = (s0 + s3) & MASK_64N;
25
+ const t = (s1 << 17n) & MASK_64N;
26
+ s2 ^= s0;
27
+ s3 ^= s1;
28
+ s1 ^= s2;
29
+ s0 ^= s3;
30
+ s2 ^= t;
31
+ s3 = rotl64(s3, 45n);
32
+ return Number(result >> 11n) / 2 ** 53;
33
+ };
34
+ }
35
+ //# sourceMappingURL=xoshiro256p.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xoshiro256p.js","sourceRoot":"","sources":["../src/xoshiro256p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAiB,MAAM,cAAc,CAAC;AAE/D;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACvC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7B,IAAI,EAAE,GAAG,OAAO,GAAG,QAAQ,CAAC;IAC5B,IAAI,EAAE,GAAG,CAAC,OAAO,GAAG,WAAW,CAAC,GAAG,QAAQ,CAAC;IAC5C,IAAI,EAAE,GAAG,CAAC,OAAO,GAAG,WAAW,CAAC,GAAG,QAAQ,CAAC;IAC5C,IAAI,EAAE,GAAG,CAAC,OAAO,GAAG,WAAW,CAAC,GAAG,QAAQ,CAAC;IAE5C,OAAO,GAAG,EAAE;QACX,MAAM,MAAM,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC;QACpC,MAAM,CAAC,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,GAAG,QAAQ,CAAC;QACjC,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,EAAE,CAAC;QACT,EAAE,IAAI,CAAC,CAAC;QACR,EAAE,GAAG,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;QACrB,OAAO,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IACxC,CAAC,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@ac-kit/math-random",
3
+ "version": "0.1.0",
4
+ "description": "Non-cryptographic pseudo-random number generators: mulberry32, pcg32, sfc32, splitmix64 and xoshiro.",
5
+ "keywords": [
6
+ "pcg",
7
+ "prng",
8
+ "random",
9
+ "splitmix",
10
+ "xoshiro"
11
+ ],
12
+ "homepage": "https://anthochamp.github.io/node-essentials/api/math-random/",
13
+ "bugs": {
14
+ "url": "https://github.com/anthochamp/node-essentials/issues"
15
+ },
16
+ "license": "BSD-3-Clause",
17
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
21
+ "directory": "packages/math-random"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "main": "./dist/index.js",
29
+ "exports": {
30
+ ".": "./dist/index.js"
31
+ },
32
+ "publishConfig": {
33
+ "main": "./dist/index.js",
34
+ "exports": {
35
+ ".": "./dist/index.js"
36
+ },
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
41
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
42
+ "clean": "rimraf dist",
43
+ "build": "yarn clean && yarn compile",
44
+ "test": "yarn run -T vitest run",
45
+ "prepack": "yarn build"
46
+ },
47
+ "dependencies": {
48
+ "@ac-kit/core": "^0.1.0",
49
+ "@types/web": "^0.0.354"
50
+ },
51
+ "devDependencies": {
52
+ "@ac-kit/tsconfig": "0.1.0",
53
+ "@ac-kit/typedoc-config": "0.1.0",
54
+ "@ac-kit/vitest-config": "0.1.0",
55
+ "@typescript/native": "typescript@7.0.2",
56
+ "rimraf": "6.1.3",
57
+ "typedoc": "0.28.20",
58
+ "typescript": "@typescript/typescript6@6.0.2"
59
+ },
60
+ "engines": {
61
+ "node": ">=24"
62
+ }
63
+ }