Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Just press Control + Shift + I and copy and paste this entire thing!
- // >inb4 somebody says it's malware
- // returns a box
- function chooseRandomBox(boxes) {
- let randomIndex = Math.floor(Math.random() * boxes.length);
- console.log('selected box: ' + boxes[randomIndex]);
- return boxes[randomIndex];
- }
- // returns a ball from box selected
- function selectBallFromBox(box) {
- let randomIndex = Math.floor(Math.random() * box.length);
- let ball = box[randomIndex];
- console.log('selected ball: ' + ball);
- box.splice(randomIndex, 1);
- return ball;
- }
- // 0 represents silver, 1 represents golden
- function doExperiment(tries) {
- let timesNextBallIsGolden = 0;
- let validTries = 0;
- for (let i = 1; i < tries + 1; i++) {
- console.log('========== TRY #' + i + ' ========== ');
- let boxes = [[0, 0], [0, 1], [1, 1]];
- let boxSelected = chooseRandomBox(boxes);
- if (selectBallFromBox(boxSelected) === 0) {
- console.log('silver ball. Retrying');
- continue;
- } else {
- // only increment tries if first ball is golden
- validTries++;
- // select second ball
- if (selectBallFromBox(boxSelected) === 1) {
- timesNextBallIsGolden++;
- }
- }
- console.log(
- 'valid tries: ' +
- validTries +
- ', timesNextBallIsGolden: ' +
- timesNextBallIsGolden +
- ' result: ' +
- timesNextBallIsGolden / tries,
- );
- }
- console.log(
- 'tries: ' +
- tries +
- ', timesNextBallIsGolden: ' +
- timesNextBallIsGolden +
- ' , result: ' +
- timesNextBallIsGolden / validTries,
- );
- }
- doExperiment(500);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement