@ac-kit/math-random 0.2.0 → 0.3.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,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 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`.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [065cf8d]
12
+ - Updated dependencies [065cf8d]
13
+ - Updated dependencies [065cf8d]
14
+ - @ac-kit/core@0.11.0
15
+ - @ac-kit/noncrypto-hash@0.2.1
16
+
3
17
  ## 0.2.0
4
18
 
5
19
  ### 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
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
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";
@@ -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,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
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";
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,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"}
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.3.0",
4
4
  "description": "Non-cryptographic pseudo-random number generators: mulberry32, pcg32, sfc32, splitmix64 and xoshiro.",
5
5
  "keywords": [
6
6
  "pcg",
@@ -45,7 +45,8 @@
45
45
  "prepack": "yarn build"
46
46
  },
47
47
  "dependencies": {
48
- "@ac-kit/core": "^0.10.0",
48
+ "@ac-kit/core": "^0.11.0",
49
+ "@ac-kit/noncrypto-hash": "^0.2.1",
49
50
  "@types/web": "^0.0.367"
50
51
  },
51
52
  "devDependencies": {