Guest User

Untitled

a guest
Feb 1st, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const rnd = Math.random;
  3. const play_round = choices => {
  4.     // Place prize behind one door
  5.     const winner = choices[rnd() * 3 | 0];
  6.  
  7.     const pickIdx = rnd() * 3 | 0;
  8.     const pick = choices[pickIdx];
  9.     choices.splice(pickIdx, 1); // Remove door from future randoms
  10.  
  11.     const showIdx = rnd() * 2 | 0;
  12.     const show = choices[showIdx];
  13.     choices.splice(showIdx, 1); // Remove door from future randoms
  14.  
  15.     // if (show == winner) {
  16.     //     // Do nothing if Monty accidentally won
  17.     // } else {
  18.     //     // Also do nothing if Monty showed an empty door
  19.     // }
  20.  
  21.     // Now that monty has shown a door, player may change their guess to final door
  22.     const pick2 = choices[0];
  23.  
  24.     // return doors (so next round does not need to re-construct the array). Order doesn't matter!
  25.     choices.push(pick);
  26.     choices.push(show);
  27.  
  28.     // Check win
  29.     return (pick == winner) | ((pick2 == winner) << 1);
  30. };
  31.  
  32. const test = _ => {
  33.     const start = performance.now();
  34.     const rounds = 10 ** 7;
  35.     const tolerance = 0.01; // 1 %
  36.  
  37.     let choices = [...Array(3).keys()];
  38.     let wins_unchanged = 0;
  39.     let wins_changed = 0;
  40.     for (_ of Array(rounds)) {
  41.         const result = play_round(choices);
  42.         wins_unchanged+= result&1;
  43.         wins_changed+= (result >> 1)&1;
  44.     }
  45.     const wins = [wins_unchanged, wins_changed];
  46.  
  47.     const win_percent = [wins_unchanged / rounds, wins_changed / rounds];
  48.     const diff = Math.abs(win_percent[0] - win_percent[1]);
  49.  
  50.     const floor2 = (v, precision) => {
  51.         const nudge = 10 ** precision;
  52.         return Math.floor(nudge * v) / nudge
  53.     }
  54.     const precision = 2;
  55.     console.log(`unchanged: ${wins_unchanged} wins; ${floor2(100 * win_percent[0], precision)} %`)
  56.     console.log(`changed: ${wins_changed} wins; ${floor2(100* win_percent[1], precision)} %`)
  57.     console.log(`difference: ${100 * diff} %`)
  58.  
  59.     if (diff < tolerance) {
  60.         console.log("Within tolerances!!! There is no difference!!!")
  61.     }
  62.     console.log(`time: ${performance.now() - start} ms`)
  63. }
  64.  
  65. test();
Advertisement
Add Comment
Please, Sign In to add comment