Pure Programmer
Blue Matrix


Cluster Map

Project: Compute π (Monte Carlo)

You can compute π (rather inefficiently) without using any trigonometric functions. By using a method called the Monte Carlo method, which generates a lot of random samples, we can approximate the value of π. If we take a square with sides of one unit and randomly generate X and Y values within the square, we can then keep track of the ones that are within one unit radius of the origin (0,0). The ratio of samples inside the quarter cirle to the total number of samples approximates the quarter circle area of the enclosing square which is π/4. Write a program that computes and prints π using the Monte Carlo method.

See [[Monte Carlo Method]]

Output
$ rustc ComputePi2.rs error: unknown format trait `f` --> ComputePi2.rs:25:36 | 25 | 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 error[E0425]: cannot find value `PI` in this scope --> ComputePi2.rs:26:60 | 26 | println!("{}", format!("abs(pi - π): {0:.10e}", fabs(pi - PI))); | ^^ | help: a local variable with a similar name exists | 26 | println!("{}", format!("abs(pi - π): {0:.10e}", fabs(pi - pi))); | ~~ help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | warning: unused import: `std::f64::consts` --> ComputePi2.rs:7:5 | 7 | use std::f64::consts; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default error[E0425]: cannot find function `random` in this scope --> ComputePi2.rs:16:16 | 16 | let x:f64 = random(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `random` in this scope --> ComputePi2.rs:17:16 | 17 | let y:f64 = random(); | ^^^^^^ not found in this scope error[E0277]: cannot multiply `f64` by `{integer}` --> ComputePi2.rs:24:61 | 24 | let pi:f64 = ((insideCount) as f64 / (MAX_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` | 24 | let pi:f64 = ((insideCount) as f64 / (MAX_SAMPLES) as f64) * 4.0; | ++ error[E0425]: cannot find function `fabs` in this scope --> ComputePi2.rs:26:50 | 26 | println!("{}", format!("abs(pi - π): {0:.10e}", fabs(pi - PI))); | ^^^^ not found in this scope error: aborting due to 6 previous errors; 1 warning emitted Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`.

Solution