Monty Hall problem: Difference between revisions
Content added Content deleted
m (added a ;Task: (bold) header.) |
|||
Line 3,234: | Line 3,234: | ||
PRINT " Staying wins ";stayWins; " times."</lang> |
PRINT " Staying wins ";stayWins; " times."</lang> |
||
=={{header|Rust}}== |
|||
<lang rust>extern crate rand; |
|||
use rand::Rng; |
|||
#[derive(Clone, Copy, PartialEq)] |
|||
enum Prize {Goat , Car} |
|||
const GAMES: usize = 3_000_000; |
|||
fn main() { |
|||
let mut switch_wins = 0; |
|||
let mut rng = rand::thread_rng(); |
|||
for _ in 0..GAMES { |
|||
let mut doors = [Prize::Goat; 3]; |
|||
*rng.choose_mut(&mut doors).unwrap() = Prize::Car; |
|||
// You only lose by switching if you pick the car the first time |
|||
if rng.choose(&doors).unwrap() != &Prize::Car { |
|||
switch_wins += 1; |
|||
} |
|||
} |
|||
println!("I played the game {total} times and won {wins} times ({percent}%).", |
|||
total = GAMES, |
|||
wins = switch_wins, |
|||
percent = switch_wins as f64 / GAMES as f64 * 100.0 |
|||
); |
|||
}</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<lang scala>import scala.util.Random |
<lang scala>import scala.util.Random |