Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3.  * Yes, this is a retarded way of doing it.
  4.  * I don't care.
  5.  */
  6. let boxes= [
  7.     ['silver', 'silver'],
  8.     ['gold', 'silver'],
  9.     ['gold', 'gold'],
  10. ];
  11.  
  12. // 2nd ball IS gold
  13. let successCount : number = 0;
  14.  
  15. // 2nd ball ISNT gold
  16. let failCount : number = 0;
  17.  
  18. // Total attempts where the first ball is gold
  19. let total : number = 0;
  20.  
  21. // Run the test 100,000 times
  22. for (let i = 0; i < 100000;  i++){
  23.  
  24.     // Choose a random box
  25.     let randomBox = Math.floor(Math.random() * 3);
  26.  
  27.     // Choose a random ball in that box
  28.     let randomBall = Math.floor(Math.random() * 2);
  29.  
  30.     // If the first ball is gold
  31.     if (boxes[randomBox][randomBall] === 'gold'){
  32.  
  33.         // If the second ball is gold
  34.         let index = randomBall === 1 ? 0 : 1;
  35.         if (boxes[randomBox][index] === 'gold'){
  36.             successCount++;
  37.         } else {
  38.             failCount++;
  39.         }
  40.         total++;
  41.     }
  42. }
  43.  
  44. // Log the ratio
  45. console.log('Success rate: ' + ((successCount / total) * 100) + '%');
  46. console.log('Fail rate: ' + ((failCount / total) * 100) + '%');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement