We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 637bce1 + f3ac45b commit 97c3c8aCopy full SHA for 97c3c8a
src/lcg.js
@@ -1,10 +1,10 @@
1
// https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
2
-const a = 1664525;
3
-const c = 1013904223;
4
-const m = 4294967296; // 2^32
+const mul = 0x19660D;
+const inc = 0x3C6EF35F;
+const eps = 1/0x100000000;
5
6
-export default function lcg(s = Math.random()) {
7
- if (!(0 <= s && s < 1)) throw new RangeError("invalid seed");
8
- s = Math.floor(m * s);
9
- return () => (s = (a * s + c) % m) / m;
+export default function lcg(seed = Math.random()) {
+ if (!(0 <= seed && seed < 1)) throw new RangeError("invalid seed");
+ let state = seed / eps | 0;
+ return () => (state = mul * state + inc | 0, eps * (state >>> 0));
10
}
0 commit comments