Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const rnd = Math.random;
- const play_round = choices => {
- // Place prize behind one door
- const winner = choices[rnd() * 3 | 0];
- const pickIdx = rnd() * 3 | 0;
- const pick = choices[pickIdx];
- choices.splice(pickIdx, 1); // Remove door from future randoms
- const showIdx = rnd() * 2 | 0;
- const show = choices[showIdx];
- choices.splice(showIdx, 1); // Remove door from future randoms
- // if (show == winner) {
- // // Do nothing if Monty accidentally won
- // } else {
- // // Also do nothing if Monty showed an empty door
- // }
- // Now that monty has shown a door, player may change their guess to final door
- const pick2 = choices[0];
- // return doors (so next round does not need to re-construct the array). Order doesn't matter!
- choices.push(pick);
- choices.push(show);
- // Check win
- return (pick == winner) | ((pick2 == winner) << 1);
- };
- const test = _ => {
- const start = performance.now();
- const rounds = 10 ** 7;
- const tolerance = 0.01; // 1 %
- let choices = [...Array(3).keys()];
- let wins_unchanged = 0;
- let wins_changed = 0;
- for (_ of Array(rounds)) {
- const result = play_round(choices);
- wins_unchanged+= result&1;
- wins_changed+= (result >> 1)&1;
- }
- const wins = [wins_unchanged, wins_changed];
- const win_percent = [wins_unchanged / rounds, wins_changed / rounds];
- const diff = Math.abs(win_percent[0] - win_percent[1]);
- const floor2 = (v, precision) => {
- const nudge = 10 ** precision;
- return Math.floor(nudge * v) / nudge
- }
- const precision = 2;
- console.log(`unchanged: ${wins_unchanged} wins; ${floor2(100 * win_percent[0], precision)} %`)
- console.log(`changed: ${wins_changed} wins; ${floor2(100* win_percent[1], precision)} %`)
- console.log(`difference: ${100 * diff} %`)
- if (diff < tolerance) {
- console.log("Within tolerances!!! There is no difference!!!")
- }
- console.log(`time: ${performance.now() - start} ms`)
- }
- test();
Advertisement
Add Comment
Please, Sign In to add comment