Created
August 18, 2019 01:17
-
-
Save winguse/643634346c009de97e087a601088b359 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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