Skip to content

Instantly share code, notes, and snippets.

@kasperpeulen
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save kasperpeulen/f3230b3279fd3157cbf2 to your computer and use it in GitHub Desktop.

Select an option

Save kasperpeulen/f3230b3279fd3157cbf2 to your computer and use it in GitHub Desktop.
Runs random simulations of the Monty Hall game and compares the two different strategies.

#dart:math dart:core example

Runs random simulations of the Monty Hall game and compares the two different strategies.

Suppose you're on a game show and you're given the choice of three doors. Behind one door is a car; behind the others, goats. The car and the goats were placed randomly behind the doors before the show. The rules of the game show are as follows: After you have chosen a door, the door remains closed for the time being. The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it. If both remaining doors have goats behind them, he chooses one randomly. After Monty Hall opens a door with a goat, he will ask you to decide whether you want to stay with your first choice or to switch to the last remaining door. Imagine that you chose Door 1 and the host opens Door 3, which has a goat. He then asks you "Do you want to switch to Door Number 2?" Is it to your advantage to change your choice? (Krauss and Wang 2003:10)

Main library: dart:math dart:core
Main elements: Random.nextInt Iterable.firstWhere Iterable.where
Gist: https://gist.github.com/kasperpeulen/f3230b3279fd3157cbf2
DartPad: https://dartpad.dartlang.org/f3230b3279fd3157cbf2
Tags: #monthy_hall

import 'dart:math';
enum Price { GOAT, CAR }
main() {
// 3 doors, behind every door there is a price.
Map<int, Price> doors = {0: Price.GOAT, 1: Price.GOAT, 2: Price.CAR};
int stayWins = 0; // tracks the amount of time the stayed won
int switchWins = 0; // tracks the amount of time the switched won
for (int i = 0; i < 100000; i++) {
// randomnly shuffle the prices
doors = new Map.fromIterables(doors.keys, doors.values.toList()..shuffle());
// randomnly guess a door
int firstGuess = new Random().nextInt(3);
// the game master will show a door that is not chosen, and doesn't contain
// the car price
int showDoor = doors.keys.where((i) => i != firstGuess && doors[i] != Price.CAR).first;
// the second guess is not the first guess and not the door shown
int secondGuess = doors.keys.firstWhere((i) => i != firstGuess && i != showDoor);
if (doors[firstGuess] == Price.CAR) stayWins += 1;
if (doors[secondGuess] == Price.CAR) switchWins += 1;
}
print('Staying wins $stayWins times.');
print('Switching wins $switchWins times.');
}
name: monty_hall
tags: monthy_hall
main_library: dart:math dart:core
main_elements: Random.nextInt Iterable.firstWhere Iterable.where
description: |
Runs random simulations of the Monty Hall game and compares the two different strategies.
Suppose you're on a game show and you're given the choice of three doors. Behind one door is a car; behind the others, goats. The car and the goats were placed randomly behind the doors before the show. The rules of the game show are as follows: After you have chosen a door, the door remains closed for the time being. The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it. If both remaining doors have goats behind them, he chooses one randomly. After Monty Hall opens a door with a goat, he will ask you to decide whether you want to stay with your first choice or to switch to the last remaining door. Imagine that you chose Door 1 and the host opens Door 3, which has a goat. He then asks you "Do you want to switch to Door Number 2?" Is it to your advantage to change your choice? (Krauss and Wang 2003:10)
homepage: https://gist.github.com/kasperpeulen/f3230b3279fd3157cbf2
environment:
sdk: '>=1.0.0 <2.0.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment