Skip to content

Instantly share code, notes, and snippets.

@kubosuke
Created March 6, 2022 03:09
Show Gist options
  • Save kubosuke/5022e4c17d43e151a4762eb77fb5bc27 to your computer and use it in GitHub Desktop.
Save kubosuke/5022e4c17d43e151a4762eb77fb5bc27 to your computer and use it in GitHub Desktop.
calc pi by rand()
use proconio::input;
use rand::prelude::*;
fn main() {
input! {
n: usize
};
let mut rng = rand::thread_rng();
let mut cnt = 0.0;
for _ in 0..n {
let x: f64 = rng.gen_range(0.0..1.0);
let y: f64 = rng.gen_range(0.0..1.0);
if x * x + y * y <= 1.0 {
cnt += 1.0;
}
}
println!("{}", 4.0 * cnt / n as f64);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment