Untitled
unknown
rust
a year ago
2.3 kB
13
Indexable
use std::process::exit;
use std::thread;
use rand;
use rand::distributions::{Standard, Distribution};
use rand::prelude::*;
#[derive(Debug, PartialEq, Eq)]
enum Drakes {
Chemtech,
Hextech,
Ocean,
Cloud,
Fireone,
Mountain,
}
impl Distribution<Drakes> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Drakes {
match rng.gen_range(0..=6) {
0 => Drakes::Chemtech,
1 => Drakes::Hextech,
2 => Drakes::Ocean,
3 => Drakes::Cloud,
4 => Drakes::Fireone,
_ => Drakes::Mountain,
}
}
}
fn monte_carlo_drakes(num_trials: u64) -> (u64, u64) {
let mut ruined = 0;
let mut unruined = 0;
for _ in 0..num_trials {
let first_drake: Drakes = rand::random();
// print!("{:?} ", first_drake);
if first_drake == Drakes::Chemtech {
unruined += 1;
// println!("fine");
continue;
}
let mut second_drake: Drakes = rand::random();
while first_drake == second_drake {
second_drake = rand::random(); // naughty indefinite postponement
}
// print!("{:?} ", second_drake);
if second_drake == Drakes::Chemtech {
unruined += 1;
// println!("fine");
continue;
}
let mut third_drake: Drakes = rand::random();
while third_drake == first_drake || third_drake == second_drake {
third_drake = rand::random(); // naughty indefinite postponement
}
// print!("{:?} ", third_drake);
if third_drake != Drakes::Chemtech {
unruined += 1;
// println!("fine");
} else {
ruined += 1;
// println!("RUINED");
}
}
(ruined, unruined)
}
fn main() {
// let o = monte_carlo_drakes(10);
// println!("{:?}", o);
//
// exit(1);
let num_threads = 16;
let num_trials = 1_000_000_000 / num_threads;
let mut handles = Vec::new();
for _ in 0..num_threads {
let h = thread::spawn(move || monte_carlo_drakes(num_trials));
handles.push(h);
}
for h in handles {
let outcomes = h.join().unwrap();
println!("{} {} {}", outcomes.0, outcomes.1, outcomes.0 as f64 / outcomes.1 as f64)
}
}Editor is loading...
Leave a Comment