@ac-kit/math-random 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,61 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [0b4fe43]
8
+ - Updated dependencies [0b4fe43]
9
+ - Updated dependencies [0b4fe43]
10
+ - Updated dependencies [0b4fe43]
11
+ - Updated dependencies [0b4fe43]
12
+ - Updated dependencies [0b4fe43]
13
+ - Updated dependencies [0b4fe43]
14
+ - Updated dependencies [0b4fe43]
15
+ - Updated dependencies [0b4fe43]
16
+ - Updated dependencies [0b4fe43]
17
+ - Updated dependencies [0b4fe43]
18
+ - Updated dependencies [0b4fe43]
19
+ - Updated dependencies [0b4fe43]
20
+ - Updated dependencies [0b4fe43]
21
+ - Updated dependencies [0b4fe43]
22
+ - Updated dependencies [0b4fe43]
23
+ - Updated dependencies [0b4fe43]
24
+ - @ac-kit/core@0.13.0
25
+ - @ac-kit/noncrypto-hash@0.2.3
26
+
27
+ ## 0.4.0
28
+
29
+ ### Minor Changes
30
+
31
+ - 34e8cfa: `randomInteger` and `randomBoolean` move from `@ac-kit/algo` to
32
+ `@ac-kit/math-random`, with unchanged behaviour. **Breaking for
33
+ `@ac-kit/algo`:** import them from `@ac-kit/math-random` instead
34
+ (`import { randomBoolean, randomInteger } from "@ac-kit/math-random"`);
35
+ `choice` and `shuffle` stay in `@ac-kit/algo`. `@ac-kit/math-stats` and
36
+ `@ac-kit/model-markup` now depend on `@ac-kit/math-random`.
37
+
38
+ ### Patch Changes
39
+
40
+ - Updated dependencies [34e8cfa]
41
+ - Updated dependencies [34e8cfa]
42
+ - Updated dependencies [34e8cfa]
43
+ - Updated dependencies [34e8cfa]
44
+ - Updated dependencies [34e8cfa]
45
+ - Updated dependencies [34e8cfa]
46
+ - Updated dependencies [34e8cfa]
47
+ - Updated dependencies [34e8cfa]
48
+ - Updated dependencies [34e8cfa]
49
+ - Updated dependencies [34e8cfa]
50
+ - Updated dependencies [34e8cfa]
51
+ - Updated dependencies [34e8cfa]
52
+ - Updated dependencies [34e8cfa]
53
+ - Updated dependencies [34e8cfa]
54
+ - Updated dependencies [34e8cfa]
55
+ - Updated dependencies [34e8cfa]
56
+ - @ac-kit/core@0.12.0
57
+ - @ac-kit/noncrypto-hash@0.2.2
58
+
3
59
  ## 0.3.0
4
60
 
5
61
  ### Minor Changes
package/README.md CHANGED
@@ -45,6 +45,18 @@ const random = pcg32(7);
45
45
  randomBytes(random, 16); // the same 16 bytes on every run
46
46
  ```
47
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
+
48
60
  **None of these is cryptographically secure.** Their output is predictable from
49
61
  a few samples by design. For keys, tokens or nonces use `@ac-kit/crypto-random`.
50
62
 
package/dist/index.d.ts CHANGED
@@ -4,6 +4,8 @@ export * from "./pcg32.js";
4
4
  export * from "./random-bytes.js";
5
5
  export * from "./sfc32.js";
6
6
  export * from "./splitmix64.js";
7
+ export * from "./variate/random-boolean.js";
8
+ export * from "./variate/random-integer.js";
7
9
  export * from "./xorshift32.js";
8
10
  export * from "./xoshiro128p.js";
9
11
  export * from "./xoshiro256p.js";
@@ -1 +1 @@
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,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
@@ -4,6 +4,8 @@ export * from "./pcg32.js";
4
4
  export * from "./random-bytes.js";
5
5
  export * from "./sfc32.js";
6
6
  export * from "./splitmix64.js";
7
+ export * from "./variate/random-boolean.js";
8
+ export * from "./variate/random-integer.js";
7
9
  export * from "./xorshift32.js";
8
10
  export * from "./xoshiro128p.js";
9
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,2BAA2B,CAAC;AAC1C,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,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.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Non-cryptographic pseudo-random number generators: mulberry32, pcg32, sfc32, splitmix64 and xoshiro.",
5
5
  "keywords": [
6
6
  "pcg",
@@ -45,14 +45,14 @@
45
45
  "prepack": "yarn build"
46
46
  },
47
47
  "dependencies": {
48
- "@ac-kit/core": "^0.11.0",
49
- "@ac-kit/noncrypto-hash": "^0.2.1",
48
+ "@ac-kit/core": "^0.13.0",
49
+ "@ac-kit/noncrypto-hash": "^0.2.3",
50
50
  "@types/web": "^0.0.367"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@ac-kit/tsconfig": "0.1.0",
54
54
  "@ac-kit/typedoc-config": "0.1.0",
55
- "@ac-kit/vitest-config": "0.1.1",
55
+ "@ac-kit/vitest-config": "0.2.0",
56
56
  "@typescript/native": "typescript@7.0.2",
57
57
  "rimraf": "6.1.4",
58
58
  "typedoc": "0.28.20",