Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Just press Control + Shift + I and copy and paste this entire thing!
  2. // >inb4 somebody says it's malware
  3. // returns a box
  4. function chooseRandomBox(boxes) {
  5.   let randomIndex = Math.floor(Math.random() * boxes.length);
  6.   console.log('selected box: ' + boxes[randomIndex]);
  7.   return boxes[randomIndex];
  8. }
  9.  
  10. // returns a ball from box selected
  11. function selectBallFromBox(box) {
  12.   let randomIndex = Math.floor(Math.random() * box.length);
  13.   let ball = box[randomIndex];
  14.   console.log('selected ball: ' + ball);
  15.   box.splice(randomIndex, 1);
  16.   return ball;
  17. }
  18. // 0 represents silver, 1 represents golden
  19. function doExperiment(tries) {
  20.   let timesNextBallIsGolden = 0;
  21.   let validTries = 0;
  22.   for (let i = 1; i < tries + 1; i++) {
  23.     console.log('========== TRY #' + i + ' ========== ');
  24.     let boxes = [[0, 0], [0, 1], [1, 1]];
  25.     let boxSelected = chooseRandomBox(boxes);
  26.     if (selectBallFromBox(boxSelected) === 0) {
  27.       console.log('silver ball. Retrying');
  28.       continue;
  29.     } else {
  30.       // only increment tries if first ball is golden
  31.       validTries++;
  32.  
  33.       // select second ball
  34.       if (selectBallFromBox(boxSelected) === 1) {
  35.         timesNextBallIsGolden++;
  36.       }
  37.     }
  38.     console.log(
  39.       'valid tries: ' +
  40.         validTries +
  41.         ', timesNextBallIsGolden: ' +
  42.         timesNextBallIsGolden +
  43.         ' result: ' +
  44.         timesNextBallIsGolden / tries,
  45.     );
  46.   }
  47.   console.log(
  48.     'tries: ' +
  49.       tries +
  50.       ', timesNextBallIsGolden: ' +
  51.       timesNextBallIsGolden +
  52.       ' , result: ' +
  53.       timesNextBallIsGolden / validTries,
  54.   );
  55. }
  56. doExperiment(500);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement