@xtia/alea-rc 0.0.7 → 0.0.8
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/entry/browser.js +1 -1
- package/internal/alea.d.ts +1 -1
- package/internal/alea.js +1 -1
- package/internal/util.d.ts +1 -1
- package/internal/util.js +13 -39
- package/package.json +1 -1
- package/prng/mulberry32.js +2 -2
- package/prng/sfc32.js +2 -2
- package/prng/xoshiro128pp.js +6 -6
package/entry/browser.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { createAleaFromByteSource } from "../internal/factories.js";
|
|
2
2
|
export * from "./common.js";
|
|
3
|
-
export const cryptoAlea = createAleaFromByteSource(arr => globalThis.crypto.getRandomValues(arr));
|
|
3
|
+
export const cryptoAlea = createAleaFromByteSource((arr) => globalThis.crypto.getRandomValues(arr));
|
package/internal/alea.d.ts
CHANGED
|
@@ -55,7 +55,7 @@ export declare class Alea {
|
|
|
55
55
|
*/
|
|
56
56
|
string(length: number, charset: string): string;
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* Generate a phrase from a table and a root string
|
|
59
59
|
* @example
|
|
60
60
|
* ```ts
|
|
61
61
|
* const message = alea.phrase({
|
package/internal/alea.js
CHANGED
package/internal/util.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function
|
|
1
|
+
export declare function hashSeed(seed: string): number;
|
package/internal/util.js
CHANGED
|
@@ -1,41 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
let
|
|
4
|
-
let
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
while (i < bytes) {
|
|
9
|
-
let k1 = (key.charCodeAt(i) & 0xff)
|
|
10
|
-
| ((key.charCodeAt(i + 1) & 0xff) << 8)
|
|
11
|
-
| ((key.charCodeAt(i + 2) & 0xff) << 16)
|
|
12
|
-
| ((key.charCodeAt(i + 3) & 0xff) << 24);
|
|
13
|
-
i += 4;
|
|
14
|
-
k1 = Math.imul(k1, c1);
|
|
15
|
-
k1 = (k1 << 15) | (k1 >>> 17);
|
|
16
|
-
k1 = Math.imul(k1, c2);
|
|
17
|
-
h1 ^= k1;
|
|
18
|
-
h1 = (h1 << 13) | (h1 >>> 19);
|
|
19
|
-
h1 = (Math.imul(h1, 5) + 0xe6546b64) | 0;
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
export function hashSeed(seed) {
|
|
3
|
+
let hash = 0;
|
|
4
|
+
for (let i = 0; i < seed.length; i++) {
|
|
5
|
+
const char = seed.charCodeAt(i);
|
|
6
|
+
hash = Math.imul(hash, 33) ^ char;
|
|
7
|
+
hash = hash | 0;
|
|
20
8
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
case 1:
|
|
28
|
-
k1 ^= (key.charCodeAt(i) & 0xff);
|
|
29
|
-
k1 = Math.imul(k1, c1);
|
|
30
|
-
k1 = (k1 << 15) | (k1 >>> 17);
|
|
31
|
-
k1 = Math.imul(k1, c2);
|
|
32
|
-
h1 ^= k1;
|
|
33
|
-
}
|
|
34
|
-
h1 ^= key.length;
|
|
35
|
-
h1 ^= h1 >>> 16;
|
|
36
|
-
h1 = Math.imul(h1, 0x85ebca6b);
|
|
37
|
-
h1 ^= h1 >>> 13;
|
|
38
|
-
h1 = Math.imul(h1, 0xc2b2ae35);
|
|
39
|
-
h1 ^= h1 >>> 16;
|
|
40
|
-
return h1 >>> 0;
|
|
9
|
+
hash ^= hash >>> 16;
|
|
10
|
+
hash = Math.imul(hash, 0x85ebca6b);
|
|
11
|
+
hash ^= hash >>> 13;
|
|
12
|
+
hash = Math.imul(hash, 0xc2b2ae35);
|
|
13
|
+
hash ^= hash >>> 16;
|
|
14
|
+
return hash >>> 0;
|
|
41
15
|
}
|
package/package.json
CHANGED
package/prng/mulberry32.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Alea } from "../internal/alea.js";
|
|
2
|
-
import {
|
|
2
|
+
import { hashSeed } from "../internal/util.js";
|
|
3
3
|
/**
|
|
4
4
|
* Create an Alea instance using a Mulberry32 source
|
|
5
5
|
*
|
|
@@ -8,7 +8,7 @@ import { murmur3_32 } from "../internal/util.js";
|
|
|
8
8
|
* @returns Alea instance using Mulberry32
|
|
9
9
|
*/
|
|
10
10
|
export const mulberry32 = (seed) => {
|
|
11
|
-
let nseed = typeof seed == "string" ?
|
|
11
|
+
let nseed = typeof seed == "string" ? hashSeed(seed) : (seed >>> 0);
|
|
12
12
|
return new Alea(() => {
|
|
13
13
|
nseed |= 0;
|
|
14
14
|
nseed = nseed + 0x6D2B79F5 | 0;
|
package/prng/sfc32.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Alea } from "../internal/alea.js";
|
|
2
|
-
import {
|
|
2
|
+
import { hashSeed } from "../internal/util.js";
|
|
3
3
|
/**
|
|
4
4
|
* Create an Alea instance using a Small Fast Counter (SFC32) source
|
|
5
5
|
*
|
|
@@ -11,7 +11,7 @@ import { murmur3_32 } from "../internal/util.js";
|
|
|
11
11
|
* @returns Alea isntance using SFC32
|
|
12
12
|
*/
|
|
13
13
|
export function sfc32(a, b, c, d) {
|
|
14
|
-
const toWord = (v) => typeof v === "number" ? (v >>> 0) :
|
|
14
|
+
const toWord = (v) => typeof v === "number" ? (v >>> 0) : hashSeed(String(v));
|
|
15
15
|
let s0 = toWord(a) | 0;
|
|
16
16
|
let s1 = toWord(b) | 0;
|
|
17
17
|
let s2 = toWord(c) | 0;
|
package/prng/xoshiro128pp.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Alea } from "../internal/alea.js";
|
|
2
|
-
import {
|
|
2
|
+
import { hashSeed } from "../internal/util.js";
|
|
3
3
|
function rotl(x, k) {
|
|
4
4
|
return (x << k) | (x >>> (32 - k));
|
|
5
5
|
}
|
|
@@ -14,11 +14,11 @@ function rotl(x, k) {
|
|
|
14
14
|
* @returns Alea instance using Xoshiro128++
|
|
15
15
|
*/
|
|
16
16
|
export function xoshiro128pp(a, b, c, d) {
|
|
17
|
-
const toWord = (v) => typeof v === "number" ? v >>> 0 :
|
|
18
|
-
let s0 = toWord(a)
|
|
19
|
-
let s1 = toWord(b)
|
|
20
|
-
let s2 = toWord(c)
|
|
21
|
-
let s3 = toWord(d)
|
|
17
|
+
const toWord = (v) => (typeof v === "number" ? v >>> 0 : hashSeed(String(v))) | 0;
|
|
18
|
+
let s0 = toWord(a);
|
|
19
|
+
let s1 = toWord(b);
|
|
20
|
+
let s2 = toWord(c);
|
|
21
|
+
let s3 = toWord(d);
|
|
22
22
|
// requires at least one non-zero value
|
|
23
23
|
if (s0 === 0 && s1 === 0 && s2 === 0 && s3 === 0) {
|
|
24
24
|
s0 = 1;
|