I was having trouble understanding the monty hall problem so I put together some Rust. I have a function which determines whether or not a participant wins a game show. If they win the game show then the function returns true, otherwise it returns false. I wanted to test for very large numbers so I added a Rayon Parallel Iterator to the for loop. The code follows:
fn gameshow(num_doors: usize, num_runs: usize, change: bool) -> usize {
let mut won_games:usize = 0;
let runs: Vec<Vec<bool>> = vec![stagehand(num_doors); num_runs]; // Creates common solution
if change {
ParallelIterator::for_each(IntoParallelIterator::into_par_iter(runs), |game| {
let winner: bool = run_game_no_change(game); // Each door is chosen randomly when tested
if winner { won_games += 1;}
});
}
else {
ParallelIterator::for_each(IntoParallelIterator::into_par_iter(runs), |game| {
let winner: bool = run_game_no_change(game); // Each door is chosen randomly when tested
if winner { won_games += 1;}
});
}
won_games
}
When compiling it throws a "Cannot assign to won_games
, as it is a captured variable in a Fn
closure" error.