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