@ac-kit/math-random 0.1.8 → 0.2.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 +24 -0
- package/README.md +10 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- 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 +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 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.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [2a0fe74]
|
|
12
|
+
- Updated dependencies [5152230]
|
|
13
|
+
- Updated dependencies [2a0fe74]
|
|
14
|
+
- Updated dependencies [5152230]
|
|
15
|
+
- Updated dependencies [5152230]
|
|
16
|
+
- Updated dependencies [2a0fe74]
|
|
17
|
+
- Updated dependencies [2a0fe74]
|
|
18
|
+
- Updated dependencies [5152230]
|
|
19
|
+
- Updated dependencies [5152230]
|
|
20
|
+
- Updated dependencies [5152230]
|
|
21
|
+
- Updated dependencies [5152230]
|
|
22
|
+
- Updated dependencies [5152230]
|
|
23
|
+
- Updated dependencies [5152230]
|
|
24
|
+
- Updated dependencies [5152230]
|
|
25
|
+
- @ac-kit/core@0.10.0
|
|
26
|
+
|
|
3
27
|
## 0.1.8
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -24,6 +24,16 @@ 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
|
+
`randomBytes(random, length)` turns any of them into a reproducible byte source,
|
|
28
|
+
for a test that needs the same "random" bytes on every run:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { pcg32, randomBytes } from "@ac-kit/math-random";
|
|
32
|
+
|
|
33
|
+
const random = pcg32(7);
|
|
34
|
+
randomBytes(random, 16); // the same 16 bytes on every run
|
|
35
|
+
```
|
|
36
|
+
|
|
27
37
|
**None of these is cryptographically secure.** Their output is predictable from
|
|
28
38
|
a few samples by design. For keys, tokens or nonces use `@ac-kit/crypto-random`.
|
|
29
39
|
|
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,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,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,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.2.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,7 @@
|
|
|
45
45
|
"prepack": "yarn build"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ac-kit/core": "^0.
|
|
48
|
+
"@ac-kit/core": "^0.10.0",
|
|
49
49
|
"@types/web": "^0.0.367"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|