@ac-kit/math-random 0.1.8 → 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 +38 -0
- package/README.md +21 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/mulberry32-from-text.d.ts +19 -0
- package/dist/mulberry32-from-text.d.ts.map +1 -0
- package/dist/mulberry32-from-text.js +23 -0
- package/dist/mulberry32-from-text.js.map +1 -0
- package/dist/random-bytes.d.ts +23 -0
- package/dist/random-bytes.d.ts.map +1 -0
- package/dist/random-bytes.js +31 -0
- package/dist/random-bytes.js.map +1 -0
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
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
|
+
|
|
17
|
+
## 0.2.0
|
|
18
|
+
|
|
19
|
+
### Minor Changes
|
|
20
|
+
|
|
21
|
+
- 2a0fe74: `randomBytes(random, length)` turns any `RandomFn` into `length` pseudo-random bytes, `Math.floor(random() * 256)` per byte, one call per byte and in order, so the same generator state gives the same bytes and a longer request extends a shorter one. It throws `RangeError` for a `length` that is not a non-negative safe integer. Not cryptographically secure: use `@ac-kit/crypto-random` for secrets.
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Updated dependencies [2a0fe74]
|
|
26
|
+
- Updated dependencies [5152230]
|
|
27
|
+
- Updated dependencies [2a0fe74]
|
|
28
|
+
- Updated dependencies [5152230]
|
|
29
|
+
- Updated dependencies [5152230]
|
|
30
|
+
- Updated dependencies [2a0fe74]
|
|
31
|
+
- Updated dependencies [2a0fe74]
|
|
32
|
+
- Updated dependencies [5152230]
|
|
33
|
+
- Updated dependencies [5152230]
|
|
34
|
+
- Updated dependencies [5152230]
|
|
35
|
+
- Updated dependencies [5152230]
|
|
36
|
+
- Updated dependencies [5152230]
|
|
37
|
+
- Updated dependencies [5152230]
|
|
38
|
+
- Updated dependencies [5152230]
|
|
39
|
+
- @ac-kit/core@0.10.0
|
|
40
|
+
|
|
3
41
|
## 0.1.8
|
|
4
42
|
|
|
5
43
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -24,6 +24,27 @@ 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
|
+
|
|
38
|
+
`randomBytes(random, length)` turns any of them into a reproducible byte source,
|
|
39
|
+
for a test that needs the same "random" bytes on every run:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { pcg32, randomBytes } from "@ac-kit/math-random";
|
|
43
|
+
|
|
44
|
+
const random = pcg32(7);
|
|
45
|
+
randomBytes(random, 16); // the same 16 bytes on every run
|
|
46
|
+
```
|
|
47
|
+
|
|
27
48
|
**None of these is cryptographically secure.** Their output is predictable from
|
|
28
49
|
a few samples by design. For keys, tokens or nonces use `@ac-kit/crypto-random`.
|
|
29
50
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -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,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
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,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"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RandomFn } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* `length` pseudo-random bytes from `random`: the byte source for a test or a
|
|
4
|
+
* replay that must give the same bytes for the same seed, where
|
|
5
|
+
* `@ac-kit/crypto-random`'s `getRandomBytes` is the one for keys, tokens and
|
|
6
|
+
* identifiers. Each byte is `Math.floor(random() * 256)`, one call to `random`
|
|
7
|
+
* per byte and in order, so two sources producing the same floats produce the
|
|
8
|
+
* same bytes, and a longer request starts with the shorter one's bytes.
|
|
9
|
+
*
|
|
10
|
+
* **Not cryptographically secure**: the bytes are as predictable as `random`.
|
|
11
|
+
*
|
|
12
|
+
* Runs in O(`length`) time and allocates the result only.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const random = pcg32(42);
|
|
16
|
+
* randomBytes(random, 4); // the same 4 bytes on every run
|
|
17
|
+
*
|
|
18
|
+
* @param random A source of floats in `[0, 1)`, such as a seeded generator.
|
|
19
|
+
* @param length How many bytes to produce: a non-negative safe integer.
|
|
20
|
+
* @throws {RangeError} When `length` is not a non-negative safe integer.
|
|
21
|
+
*/
|
|
22
|
+
export declare function randomBytes(random: RandomFn, length: number): Uint8Array<ArrayBuffer>;
|
|
23
|
+
//# sourceMappingURL=random-bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-bytes.d.ts","sourceRoot":"","sources":["../src/random-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,WAAW,CAC1B,MAAM,EAAE,QAAQ,EAChB,MAAM,EAAE,MAAM,GACZ,UAAU,CAAC,WAAW,CAAC,CAczB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `length` pseudo-random bytes from `random`: the byte source for a test or a
|
|
3
|
+
* replay that must give the same bytes for the same seed, where
|
|
4
|
+
* `@ac-kit/crypto-random`'s `getRandomBytes` is the one for keys, tokens and
|
|
5
|
+
* identifiers. Each byte is `Math.floor(random() * 256)`, one call to `random`
|
|
6
|
+
* per byte and in order, so two sources producing the same floats produce the
|
|
7
|
+
* same bytes, and a longer request starts with the shorter one's bytes.
|
|
8
|
+
*
|
|
9
|
+
* **Not cryptographically secure**: the bytes are as predictable as `random`.
|
|
10
|
+
*
|
|
11
|
+
* Runs in O(`length`) time and allocates the result only.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const random = pcg32(42);
|
|
15
|
+
* randomBytes(random, 4); // the same 4 bytes on every run
|
|
16
|
+
*
|
|
17
|
+
* @param random A source of floats in `[0, 1)`, such as a seeded generator.
|
|
18
|
+
* @param length How many bytes to produce: a non-negative safe integer.
|
|
19
|
+
* @throws {RangeError} When `length` is not a non-negative safe integer.
|
|
20
|
+
*/
|
|
21
|
+
export function randomBytes(random, length) {
|
|
22
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
23
|
+
throw new RangeError(`randomBytes: length ${length} is not a non-negative integer`);
|
|
24
|
+
}
|
|
25
|
+
const bytes = new Uint8Array(length);
|
|
26
|
+
for (let index = 0; index < length; index++) {
|
|
27
|
+
bytes[index] = Math.floor(random() * 256);
|
|
28
|
+
}
|
|
29
|
+
return bytes;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=random-bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-bytes.js","sourceRoot":"","sources":["../src/random-bytes.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,WAAW,CAC1B,MAAgB,EAChB,MAAc;IAEd,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,UAAU,CACnB,uBAAuB,MAAM,gCAAgC,CAC7D,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAErC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/math-random",
|
|
3
|
-
"version": "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.
|
|
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": {
|