Skip to content

Instantly share code, notes, and snippets.

@winguse
Created August 18, 2019 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winguse/643634346c009de97e087a601088b359 to your computer and use it in GitHub Desktop.
Save winguse/643634346c009de97e087a601088b359 to your computer and use it in GitHub Desktop.
const p = 0.723;
function oldRandom() {
return Math.random() < p ? 1 : 0;
}
const table = [[1, 2], [2, 3], [1, 3]];
let flag = 0;
function newRandom() {
const f = flag ++;
const r = oldRandom();
return table[f % 3][((Math.floor(f / 3) & 1) ^ r )];
}
const test = 1000000;
const m = [0, 0, 0, 0];
for (let i = 0; i < test; i++) {
m[newRandom()]++;
}
console.log(m.map(v => v / test));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment