Skip to content

Commit

Permalink
speed up 10x (Safari) or 4x (Chrome/Firefox)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrus committed Aug 22, 2020
1 parent 637bce1 commit ca0a54d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/lcg.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
const a = 1664525;
const c = 1013904223;
const m = 4294967296; // 2^32
const mul = 0x19660D;
const inc = 0x3C6EF35F;
const lo32 = 0xFFFFFFFF;
const eps = Math.pow(2, -32);

export default function lcg(s = Math.random()) {
if (!(0 <= s && s < 1)) throw new RangeError("invalid seed");
s = Math.floor(m * s);
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 = Math.floor((1+lo32) * seed);
return function random () {
return eps * ((state = (mul * state + inc) & lo32) >>> 0);
};
}

0 comments on commit ca0a54d

Please sign in to comment.