@@ -95,12 +95,11 @@ use std::rand::distributions::{IndependentSample, Range};
95
95
96
96
struct SimulationResult {
97
97
win: bool,
98
- switch: bool
98
+ switch: bool,
99
99
}
100
100
101
101
// Run a single simulation of the Monty Hall problem.
102
- fn simulate<R: Rng>(rng: &mut R) -> SimulationResult {
103
- let random_door = Range::new(0u, 3);
102
+ fn simulate<R: Rng>(random_door: &Range<uint>, rng: &mut R) -> SimulationResult {
104
103
let car = random_door.ind_sample(rng);
105
104
106
105
// This is our initial choice
@@ -121,32 +120,33 @@ fn simulate<R: Rng>(rng: &mut R) -> SimulationResult {
121
120
// Returns the door the game host opens given our choice and knowledge of
122
121
// where the car is. The game host will never open the door with the car.
123
122
fn game_host_open<R: Rng>(car: uint, choice: uint, rng: &mut R) -> uint {
124
- let choices = free_doors(vec! [car, choice]);
123
+ let choices = free_doors(& [car, choice]);
125
124
rand::sample(rng, choices.move_iter(), 1)[0]
126
125
}
127
126
128
127
// Returns the door we switch to, given our current choice and
129
128
// the open door. There will only be one valid door.
130
129
fn switch_door(choice: uint, open: uint) -> uint {
131
- free_doors(vec! [choice, open])[0]
130
+ free_doors(& [choice, open])[0]
132
131
}
133
132
134
- fn free_doors(blocked: Vec< uint> ) -> Vec<uint> {
133
+ fn free_doors(blocked: &[ uint] ) -> Vec<uint> {
135
134
range(0u, 3).filter(|x| !blocked.contains(x)).collect()
136
135
}
137
136
138
137
fn main() {
139
- // The estimation will be more accuraty with more simulations
138
+ // The estimation will be more accurate with more simulations
140
139
let num_simulations = 10000u;
141
140
142
141
let mut rng = rand::task_rng();
142
+ let random_door = Range::new(0u, 3);
143
143
144
144
let (mut switch_wins, mut switch_losses) = (0u, 0u);
145
145
let (mut keep_wins, mut keep_losses) = (0u, 0u);
146
146
147
147
println!("Running {} simulations...", num_simulations);
148
148
for _ in range(0, num_simulations) {
149
- let result = simulate(&mut rng);
149
+ let result = simulate(&random_door, & mut rng);
150
150
151
151
match (result.win, result.switch) {
152
152
(true, true) => switch_wins += 1,
0 commit comments