]>
git.proxmox.com Git - rustc.git/blob - vendor/rand/examples/monty-hall.rs
1 // Copyright 2018 Developers of the Rand project.
2 // Copyright 2013-2018 The Rust Project Developers.
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.
10 //! ## Monty Hall Problem
12 //! This is a simulation of the [Monty Hall Problem][]:
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?
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
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.
27 //! [Monty Hall Problem]: https://en.wikipedia.org/wiki/Monty_Hall_problem
29 #![cfg(feature = "std")]
31 use rand
::distributions
::{Distribution, Uniform}
;
34 struct SimulationResult
{
39 // Run a single simulation of the Monty Hall problem.
40 fn simulate
<R
: Rng
>(random_door
: &Uniform
<u32>, rng
: &mut R
) -> SimulationResult
{
41 let car
= random_door
.sample(rng
);
43 // This is our initial choice
44 let mut choice
= random_door
.sample(rng
);
46 // The game host opens a door
47 let open
= game_host_open(car
, choice
, rng
);
50 let switch
= rng
.gen();
52 choice
= switch_door(choice
, open
);
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.
63 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()
68 // Returns the door we switch to, given our current choice and
69 // the open door. There will only be one valid door.
70 fn switch_door(choice
: u32, open
: u32) -> u32 {
71 free_doors(&[choice
, open
])[0]
74 fn free_doors(blocked
: &[u32]) -> Vec
<u32> {
75 (0..3).filter(|x
| !blocked
.contains(x
)).collect()
79 // The estimation will be more accurate with more simulations
80 let num_simulations
= 10000;
82 let mut rng
= rand
::thread_rng();
83 let random_door
= Uniform
::new(0u32, 3);
85 let (mut switch_wins
, mut switch_losses
) = (0, 0);
86 let (mut keep_wins
, mut keep_losses
) = (0, 0);
88 println
!("Running {} simulations...", num_simulations
);
89 for _
in 0..num_simulations
{
90 let result
= simulate(&random_door
, &mut rng
);
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,
100 let total_switches
= switch_wins
+ switch_losses
;
101 let total_keeps
= keep_wins
+ keep_losses
;
104 "Switched door {} times with {} wins and {} losses",
105 total_switches
, switch_wins
, switch_losses
109 "Kept our choice {} times with {} wins and {} losses",
110 total_keeps
, keep_wins
, keep_losses
113 // With a large number of simulations, the values should converge to
114 // 0.667 and 0.333 respectively.
116 "Estimated chance to win if we switch: {}",
117 switch_wins
as f32 / total_switches
as f32
120 "Estimated chance to win if we don't: {}",
121 keep_wins
as f32 / total_keeps
as f32