Pure Programmer
Blue Matrix


Cluster Map

Project: Compute π (Monte Carlo with Central Limit Theorem)

Write a program that computes and prints π using the Monte Carlo method. See ComputePi2. Compute π 20 times using the Monte Carlo method and take the average to get a better approximation. Use a function to encapsulate the Monte Carlo simulation.

The idea that the average of multiple trials yields a better approximation is called the [[Central Limit Theorem]]. Is this approach any better than just computing one estimate with a larger number of samples?

Output
$ rustc ComputePi4.rs error: unknown format trait `d` --> ComputePi4.rs:34:33 | 34 | println!("{}", format!("PI{0:d}: {1:.15f}", i, p)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> ComputePi4.rs:34:43 | 34 | println!("{}", format!("PI{0:d}: {1:.15f}", i, p)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> ComputePi4.rs:40:36 | 40 | println!("{}", format!("PI: {0:.15f}", PI)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait warning: unused import: `std::f64::consts` --> ComputePi4.rs:8:5 | 8 | use std::f64::consts; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default error[E0425]: cannot find function `random` in this scope --> ComputePi4.rs:17:16 | 17 | let x:f64 = random(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `random` in this scope --> ComputePi4.rs:18:16 | 18 | let y:f64 = random(); | ^^^^^^ not found in this scope error[E0277]: cannot multiply `f64` by `{integer}` --> ComputePi4.rs:25:51 | 25 | return ((insideCount) as f64 / (samples) as f64) * 4; | ^ no implementation for `f64 * {integer}` | = help: the trait `Mul<{integer}>` is not implemented for `f64` = help: the following other types implement trait `Mul<Rhs>`: <&'a f64 as Mul<f64>> <&f64 as Mul<&f64>> <f64 as Mul<&f64>> <f64 as Mul> help: consider using a floating-point literal by writing it with `.0` | 25 | return ((insideCount) as f64 / (samples) as f64) * 4.0; | ++ error[E0425]: cannot find function `fabs` in this scope --> ComputePi4.rs:41:50 | 41 | println!("{}", format!("abs(PI - π): {0:.10e}", fabs(PI - PI))); | ^^^^ not found in this scope error: aborting due to 7 previous errors; 1 warning emitted Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`.

Solution