7.6 Monte Carlo and randomness
Explanation
Monte Carlo methods use randomness to estimate quantities. A Monte Carlo result should include the random seed, sample size, estimated uncertainty, and a check that the result changes sensibly with more samples.
In Rust, use an RNG crate such as rand. Computers usually generate pseudorandom numbers, not truly random numbers. A pseudorandom number generator follows a deterministic algorithm. The seed selects the starting point of the sequence. If the seed, algorithm, and code are the same, the same sequence can be reproduced.
The seed policy matters. A fixed seed is useful for debugging and reproducibility, but it does not remove sampling error. For a scientific claim, report uncertainty instead of trusting one seed.
Put the main sampling work in a function. A function makes the computation easier to test, rerun with different parameters, and separate from printing, file output, or plotting.
Things to look up
- Monte Carlo method
- Pseudorandom number generator
- Random seed
- Sampling error
- Error bar
- Law of large numbers
rand
Exercise
Estimate pi in Rust by sampling random points in a square and counting how many fall inside the unit circle.
Run two sample sizes, such as 10_000 and 1_000_000. For each run, record:
- The command you ran.
- The seed or seed policy.
- The sample size.
- The estimate.
- A simple uncertainty estimate or repeated-seed comparison.
Notes for the exercise
- Multiple seeds or uncertainty estimates matter for scientific claims.
- Save the result and metadata before plotting if a plot is made.
- Put the repeated sampling loop in a function.
- Keep plotting, printing, and file output outside the core computation when possible.
- Explain how the error should change as the sample size increases.