@ac-kit/math-random 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,51 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 34e8cfa: `randomInteger` and `randomBoolean` move from `@ac-kit/algo` to
8
+ `@ac-kit/math-random`, with unchanged behaviour. **Breaking for
9
+ `@ac-kit/algo`:** import them from `@ac-kit/math-random` instead
10
+ (`import { randomBoolean, randomInteger } from "@ac-kit/math-random"`);
11
+ `choice` and `shuffle` stay in `@ac-kit/algo`. `@ac-kit/math-stats` and
12
+ `@ac-kit/model-markup` now depend on `@ac-kit/math-random`.
13
+
14
+ ### Patch Changes
15
+
16
+ - Updated dependencies [34e8cfa]
17
+ - Updated dependencies [34e8cfa]
18
+ - Updated dependencies [34e8cfa]
19
+ - Updated dependencies [34e8cfa]
20
+ - Updated dependencies [34e8cfa]
21
+ - Updated dependencies [34e8cfa]
22
+ - Updated dependencies [34e8cfa]
23
+ - Updated dependencies [34e8cfa]
24
+ - Updated dependencies [34e8cfa]
25
+ - Updated dependencies [34e8cfa]
26
+ - Updated dependencies [34e8cfa]
27
+ - Updated dependencies [34e8cfa]
28
+ - Updated dependencies [34e8cfa]
29
+ - Updated dependencies [34e8cfa]
30
+ - Updated dependencies [34e8cfa]
31
+ - Updated dependencies [34e8cfa]
32
+ - @ac-kit/core@0.12.0
33
+ - @ac-kit/noncrypto-hash@0.2.2
34
+
35
+ ## 0.3.0
36
+
37
+ ### Minor Changes
38
+
39
+ - 065cf8d: New `mulberry32FromText(label)`: a `mulberry32` generator seeded by the `djb2a` hash of the label's UTF-8 bytes, so the same text always gives the same sequence. The package now depends on `@ac-kit/noncrypto-hash`.
40
+
41
+ ### Patch Changes
42
+
43
+ - Updated dependencies [065cf8d]
44
+ - Updated dependencies [065cf8d]
45
+ - Updated dependencies [065cf8d]
46
+ - @ac-kit/core@0.11.0
47
+ - @ac-kit/noncrypto-hash@0.2.1
48
+
3
49
  ## 0.2.0
4
50
 
5
51
  ### Minor Changes
package/README.md CHANGED
@@ -24,6 +24,17 @@ such as `@ac-kit/algo`'s `shuffle`.
24
24
  Available: `mulberry32`, `pcg32`, `sfc32`, `splitmix64`, `xorshift32`,
25
25
  `xoshiro128p`, `xoshiro256p` — differing in state size, period and speed.
26
26
 
27
+ `mulberry32FromText(label)` seeds `mulberry32` from a text instead of a
28
+ number — the `djb2a` hash of the label's UTF-8 bytes — so a page slug or a level
29
+ name always gives the same sequence:
30
+
31
+ ```ts
32
+ import { mulberry32FromText } from "@ac-kit/math-random";
33
+
34
+ const random = mulberry32FromText("hello");
35
+ random(); // 0.06705552712082863, on every run
36
+ ```
37
+
27
38
  `randomBytes(random, length)` turns any of them into a reproducible byte source,
28
39
  for a test that needs the same "random" bytes on every run:
29
40
 
@@ -34,6 +45,18 @@ const random = pcg32(7);
34
45
  randomBytes(random, 16); // the same 16 bytes on every run
35
46
  ```
36
47
 
48
+ `randomInteger(minInclusive, maxExclusive, random)` and
49
+ `randomBoolean(probability, random)` shape any of them into one integer in a
50
+ range or one biased coin flip, each consuming exactly one draw:
51
+
52
+ ```ts
53
+ import { mulberry32, randomBoolean, randomInteger } from "@ac-kit/math-random";
54
+
55
+ const random = mulberry32(1);
56
+ randomInteger(1, 7, random); // a die roll, 1..6
57
+ randomBoolean(0.3, random); // true three times out of ten
58
+ ```
59
+
37
60
  **None of these is cryptographically secure.** Their output is predictable from
38
61
  a few samples by design. For keys, tokens or nonces use `@ac-kit/crypto-random`.
39
62
 
package/dist/index.d.ts CHANGED
@@ -1,8 +1,11 @@
1
+ export * from "./mulberry32-from-text.js";
1
2
  export * from "./mulberry32.js";
2
3
  export * from "./pcg32.js";
3
4
  export * from "./random-bytes.js";
4
5
  export * from "./sfc32.js";
5
6
  export * from "./splitmix64.js";
7
+ export * from "./variate/random-boolean.js";
8
+ export * from "./variate/random-integer.js";
6
9
  export * from "./xorshift32.js";
7
10
  export * from "./xoshiro128p.js";
8
11
  export * from "./xoshiro256p.js";
@@ -1 +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,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -1,8 +1,11 @@
1
+ export * from "./mulberry32-from-text.js";
1
2
  export * from "./mulberry32.js";
2
3
  export * from "./pcg32.js";
3
4
  export * from "./random-bytes.js";
4
5
  export * from "./sfc32.js";
5
6
  export * from "./splitmix64.js";
7
+ export * from "./variate/random-boolean.js";
8
+ export * from "./variate/random-integer.js";
6
9
  export * from "./xorshift32.js";
7
10
  export * from "./xoshiro128p.js";
8
11
  export * from "./xoshiro256p.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { type RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * A {@link mulberry32} generator seeded by a text label: the label's UTF-8 bytes
4
+ * hashed with `djb2a` from `@ac-kit/noncrypto-hash` give the 32-bit seed, so
5
+ * the same label always yields the same sequence, on every machine. Reach for
6
+ * it to derive a reproducible sequence from a name — a page's slug, a level's
7
+ * title — rather than to pick a numeric seed.
8
+ *
9
+ * Distinct labels may share a seed (the hash has 2³² values), and so a
10
+ * sequence. Not cryptographically secure.
11
+ *
12
+ * Time and memory O(label length) to seed; each draw is O(1).
13
+ *
14
+ * @example
15
+ * const random = mulberry32FromText("hello");
16
+ * random(); // 0.06705552712082863, on every run
17
+ */
18
+ export declare function mulberry32FromText(label: string): RandomFn;
19
+ //# sourceMappingURL=mulberry32-from-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mulberry32-from-text.d.ts","sourceRoot":"","sources":["../src/mulberry32-from-text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAK7D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,QAAQ,CAE1D"}
@@ -0,0 +1,23 @@
1
+ import { encodeTextUtf8 } from "@ac-kit/core";
2
+ import { djb2a } from "@ac-kit/noncrypto-hash";
3
+ import { mulberry32 } from "./mulberry32.js";
4
+ /**
5
+ * A {@link mulberry32} generator seeded by a text label: the label's UTF-8 bytes
6
+ * hashed with `djb2a` from `@ac-kit/noncrypto-hash` give the 32-bit seed, so
7
+ * the same label always yields the same sequence, on every machine. Reach for
8
+ * it to derive a reproducible sequence from a name — a page's slug, a level's
9
+ * title — rather than to pick a numeric seed.
10
+ *
11
+ * Distinct labels may share a seed (the hash has 2³² values), and so a
12
+ * sequence. Not cryptographically secure.
13
+ *
14
+ * Time and memory O(label length) to seed; each draw is O(1).
15
+ *
16
+ * @example
17
+ * const random = mulberry32FromText("hello");
18
+ * random(); // 0.06705552712082863, on every run
19
+ */
20
+ export function mulberry32FromText(label) {
21
+ return mulberry32(djb2a(encodeTextUtf8(label)));
22
+ }
23
+ //# sourceMappingURL=mulberry32-from-text.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mulberry32-from-text.js","sourceRoot":"","sources":["../src/mulberry32-from-text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAiB,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAE/C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC/C,OAAO,UAAU,CAAC,KAAK,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACjD,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Answers `true` with the given probability.
4
+ *
5
+ * Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
6
+ * a seeded source is consumed at the same rate whichever branch a test takes.
7
+ * `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
8
+ *
9
+ * Complexity: O(1), one call to `random`.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * if (randomBoolean(0.3, Math.random)) {
14
+ * // taken three times out of ten
15
+ * }
16
+ * ```;
17
+ *
18
+ * @param probability The chance of `true`, in `[0, 1]`.
19
+ * @param random A source in `[0, 1)`.
20
+ * @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
21
+ */
22
+ export declare function randomBoolean(probability: number, random: RandomFn): boolean;
23
+ //# sourceMappingURL=random-boolean.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random-boolean.d.ts","sourceRoot":"","sources":["../../src/variate/random-boolean.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,OAAO,CAM5E"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Answers `true` with the given probability.
3
+ *
4
+ * Calls `random` exactly once, whatever `probability` is (also `0` and `1`), so
5
+ * a seeded source is consumed at the same rate whichever branch a test takes.
6
+ * `0` never answers `true` and `1` always does, since `random` is in `[0, 1)`.
7
+ *
8
+ * Complexity: O(1), one call to `random`.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * if (randomBoolean(0.3, Math.random)) {
13
+ * // taken three times out of ten
14
+ * }
15
+ * ```;
16
+ *
17
+ * @param probability The chance of `true`, in `[0, 1]`.
18
+ * @param random A source in `[0, 1)`.
19
+ * @throws {RangeError} When `probability` is `NaN` or outside `[0, 1]`.
20
+ */
21
+ export function randomBoolean(probability, random) {
22
+ if (!(probability >= 0 && probability <= 1)) {
23
+ throw new RangeError("probability must be within [0, 1]");
24
+ }
25
+ return random() < probability;
26
+ }
27
+ //# sourceMappingURL=random-boolean.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random-boolean.js","sourceRoot":"","sources":["../../src/variate/random-boolean.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,MAAgB;IAClE,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CAAC,mCAAmC,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,MAAM,EAAE,GAAG,WAAW,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { RandomFn } from "@ac-kit/core";
2
+ /**
3
+ * Draws an integer uniformly from `[minInclusive, maxExclusive)`.
4
+ *
5
+ * Computed as `floor(random() * range)`, so the result is as uniform as
6
+ * `random` is: a generator with `k` bits of output gives each value a
7
+ * probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
8
+ * `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
9
+ * range wider than the generator's resolution cannot be covered uniformly by
10
+ * any method.
11
+ *
12
+ * Complexity: O(1), one call to `random`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const roll = randomInteger(1, 7, Math.random); // 1..6
17
+ * ```;
18
+ *
19
+ * @param minInclusive The smallest result; a safe integer.
20
+ * @param maxExclusive The bound, never returned; a safe integer greater than
21
+ * `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
22
+ * @param random A source in `[0, 1)`.
23
+ * @throws {RangeError} When a bound is not a safe integer, the range is empty
24
+ * or wider than `2^53`.
25
+ */
26
+ export declare function randomInteger(minInclusive: number, maxExclusive: number, random: RandomFn): number;
27
+ //# sourceMappingURL=random-integer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random-integer.d.ts","sourceRoot":"","sources":["../../src/variate/random-integer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAC5B,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,QAAQ,GACd,MAAM,CAkBR"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Draws an integer uniformly from `[minInclusive, maxExclusive)`.
3
+ *
4
+ * Computed as `floor(random() * range)`, so the result is as uniform as
5
+ * `random` is: a generator with `k` bits of output gives each value a
6
+ * probability within `range / 2^k` (relative) of `1 / range`, about `2^-20` for
7
+ * `range = 2^32` over `Math.random`'s 52 to 53 bits, depending on the engine. A
8
+ * range wider than the generator's resolution cannot be covered uniformly by
9
+ * any method.
10
+ *
11
+ * Complexity: O(1), one call to `random`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const roll = randomInteger(1, 7, Math.random); // 1..6
16
+ * ```;
17
+ *
18
+ * @param minInclusive The smallest result; a safe integer.
19
+ * @param maxExclusive The bound, never returned; a safe integer greater than
20
+ * `minInclusive`, with `maxExclusive - minInclusive` at most `2^53`.
21
+ * @param random A source in `[0, 1)`.
22
+ * @throws {RangeError} When a bound is not a safe integer, the range is empty
23
+ * or wider than `2^53`.
24
+ */
25
+ export function randomInteger(minInclusive, maxExclusive, random) {
26
+ if (!Number.isSafeInteger(minInclusive) ||
27
+ !Number.isSafeInteger(maxExclusive)) {
28
+ throw new RangeError("bounds must be safe integers");
29
+ }
30
+ // Compared before subtracting: the difference of two safe integers can
31
+ // round to 2^53 when the true range is 2^53 + 1.
32
+ if (!(maxExclusive > minInclusive) || maxExclusive > minInclusive + 2 ** 53) {
33
+ throw new RangeError("maxExclusive must exceed minInclusive by at most 2^53");
34
+ }
35
+ const range = maxExclusive - minInclusive;
36
+ const offset = Math.floor(random() * range);
37
+ // random() * range can round up to range itself for a huge range.
38
+ return minInclusive + Math.min(offset, range - 1);
39
+ }
40
+ //# sourceMappingURL=random-integer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random-integer.js","sourceRoot":"","sources":["../../src/variate/random-integer.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,aAAa,CAC5B,YAAoB,EACpB,YAAoB,EACpB,MAAgB;IAEhB,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC;QACnC,CAAC,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,EAClC,CAAC;QACF,MAAM,IAAI,UAAU,CAAC,8BAA8B,CAAC,CAAC;IACtD,CAAC;IACD,uEAAuE;IACvE,iDAAiD;IACjD,IAAI,CAAC,CAAC,YAAY,GAAG,YAAY,CAAC,IAAI,YAAY,GAAG,YAAY,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;QAC7E,MAAM,IAAI,UAAU,CACnB,uDAAuD,CACvD,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,YAAY,GAAG,YAAY,CAAC;IAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IAC5C,kEAAkE;IAClE,OAAO,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;AACnD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/math-random",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Non-cryptographic pseudo-random number generators: mulberry32, pcg32, sfc32, splitmix64 and xoshiro.",
5
5
  "keywords": [
6
6
  "pcg",
@@ -45,13 +45,14 @@
45
45
  "prepack": "yarn build"
46
46
  },
47
47
  "dependencies": {
48
- "@ac-kit/core": "^0.10.0",
48
+ "@ac-kit/core": "^0.12.0",
49
+ "@ac-kit/noncrypto-hash": "^0.2.2",
49
50
  "@types/web": "^0.0.367"
50
51
  },
51
52
  "devDependencies": {
52
53
  "@ac-kit/tsconfig": "0.1.0",
53
54
  "@ac-kit/typedoc-config": "0.1.0",
54
- "@ac-kit/vitest-config": "0.1.1",
55
+ "@ac-kit/vitest-config": "0.2.0",
55
56
  "@typescript/native": "typescript@7.0.2",
56
57
  "rimraf": "6.1.4",
57
58
  "typedoc": "0.28.20",