1 // Copyright 2018 Developers of the Rand project.
2 // Copyright 2013-2018 The Rust Project Developers.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9 
10 //! ## Monty Hall Problem
11 //!
12 //! This is a simulation of the [Monty Hall Problem][]:
13 //!
14 //! > Suppose you're on a game show, and you're given the choice of three doors:
15 //! > Behind one door is a car; behind the others, goats. You pick a door, say
16 //! > No. 1, and the host, who knows what's behind the doors, opens another
17 //! > door, say No. 3, which has a goat. He then says to you, "Do you want to
18 //! > pick door No. 2?" Is it to your advantage to switch your choice?
19 //!
20 //! The rather unintuitive answer is that you will have a 2/3 chance of winning
21 //! if you switch and a 1/3 chance of winning if you don't, so it's better to
22 //! switch.
23 //!
24 //! This program will simulate the game show and with large enough simulation
25 //! steps it will indeed confirm that it is better to switch.
26 //!
27 //! [Monty Hall Problem]: https://en.wikipedia.org/wiki/Monty_Hall_problem
28 
29 #![cfg(feature = "std")]
30 
31 use rand::distributions::{Distribution, Uniform};
32 use rand::Rng;
33 
34 struct SimulationResult {
35     win: bool,
36     switch: bool,
37 }
38 
39 // Run a single simulation of the Monty Hall problem.
simulate<R: Rng>(random_door: &Uniform<u32>, rng: &mut R) -> SimulationResult40 fn simulate<R: Rng>(random_door: &Uniform<u32>, rng: &mut R) -> SimulationResult {
41     let car = random_door.sample(rng);
42 
43     // This is our initial choice
44     let mut choice = random_door.sample(rng);
45 
46     // The game host opens a door
47     let open = game_host_open(car, choice, rng);
48 
49     // Shall we switch?
50     let switch = rng.gen();
51     if switch {
52         choice = switch_door(choice, open);
53     }
54 
55     SimulationResult {
56         win: choice == car,
57         switch,
58     }
59 }
60 
61 // Returns the door the game host opens given our choice and knowledge of
62 // where the car is. The game host will never open the door with the car.
game_host_open<R: Rng>(car: u32, choice: u32, rng: &mut R) -> u3263 fn game_host_open<R: Rng>(car: u32, choice: u32, rng: &mut R) -> u32 {
64     use rand::seq::SliceRandom;
65     *free_doors(&[car, choice]).choose(rng).unwrap()
66 }
67 
68 // Returns the door we switch to, given our current choice and
69 // the open door. There will only be one valid door.
switch_door(choice: u32, open: u32) -> u3270 fn switch_door(choice: u32, open: u32) -> u32 {
71     free_doors(&[choice, open])[0]
72 }
73 
free_doors(blocked: &[u32]) -> Vec<u32>74 fn free_doors(blocked: &[u32]) -> Vec<u32> {
75     (0..3).filter(|x| !blocked.contains(x)).collect()
76 }
77 
main()78 fn main() {
79     // The estimation will be more accurate with more simulations
80     let num_simulations = 10000;
81 
82     let mut rng = rand::thread_rng();
83     let random_door = Uniform::new(0u32, 3);
84 
85     let (mut switch_wins, mut switch_losses) = (0, 0);
86     let (mut keep_wins, mut keep_losses) = (0, 0);
87 
88     println!("Running {} simulations...", num_simulations);
89     for _ in 0..num_simulations {
90         let result = simulate(&random_door, &mut rng);
91 
92         match (result.win, result.switch) {
93             (true, true) => switch_wins += 1,
94             (true, false) => keep_wins += 1,
95             (false, true) => switch_losses += 1,
96             (false, false) => keep_losses += 1,
97         }
98     }
99 
100     let total_switches = switch_wins + switch_losses;
101     let total_keeps = keep_wins + keep_losses;
102 
103     println!(
104         "Switched door {} times with {} wins and {} losses",
105         total_switches, switch_wins, switch_losses
106     );
107 
108     println!(
109         "Kept our choice {} times with {} wins and {} losses",
110         total_keeps, keep_wins, keep_losses
111     );
112 
113     // With a large number of simulations, the values should converge to
114     // 0.667 and 0.333 respectively.
115     println!(
116         "Estimated chance to win if we switch: {}",
117         switch_wins as f32 / total_switches as f32
118     );
119     println!(
120         "Estimated chance to win if we don't: {}",
121         keep_wins as f32 / total_keeps as f32
122     );
123 }
124