程式語言 - Rust - PI - Monte Carlo



參考資訊:
https://www.rust-lang.org/learn/get-started
http://bzz.wallizard.com:8081/share/books/RUST/Programming%20Rust%202nd%20Edition.pdf
https://www.soroushjp.com/2015/02/07/go-concurrency-is-not-parallelism-real-world-lessons-with-monte-carlo-simulations/

產生樣板

$ cargo new hello
$ cd hello

Cargo.toml

[package]
name = "hello"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8"

src/main.rs

use rand::Rng;

fn _pi(samples : u32) -> f64 {
    let mut inside : u32 = 0;
    let mut rng = rand::thread_rng();

    for _ in 0..samples {
        let x : f64 = (rng.gen::<u32>() as f64) / f64::from(u32::MAX);
        let y : f64 = (rng.gen::<u32>() as f64) / f64::from(u32::MAX);

        if ((x * x) + (y * y)) < 1.0 {
            inside = inside + 1;
        }
    }

    if inside > 0 {
        return (f64::from(inside) / f64::from(samples)) * 4.0;
    }
    return 0.0;
}

fn main() {
    for i in 2..=8 {
        let s = u32::pow(10, i);
        println!("Our value of Pi after {} runs: {}", s, _pi(s));
    }
}

執行

$ cargo run
    Our value of Pi after 100 runs: 3.12
    Our value of Pi after 1000 runs: 3.116
    Our value of Pi after 10000 runs: 3.1584
    Our value of Pi after 100000 runs: 3.1444
    Our value of Pi after 1000000 runs: 3.14116
    Our value of Pi after 10000000 runs: 3.1411176
    Our value of Pi after 100000000 runs: 3.14145844