Designing a Monte Carlo Simulation in R
Instruction: Write a function in R that performs a Monte Carlo simulation to estimate the value of Pi.
Context: This question assesses the candidate's understanding of statistical simulation techniques and their ability to implement them in R for solving complex problems.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Sample independent points uniformly from the square [-1, 1] × [-1, 1], whose area is 4. The full unit disk has area π, so the probability that x² + y² ≤ 1 is π/4. Four times the observed hit fraction estimates π.
```r estimatePi <- function(n) { if (length(n) != 1L || !is.numeric(n) || is.na(n) || !is.finite(n) || n < 1 || n != floor(n) || n > .Machine$integer.max) { stop("n must be one positive finite integer within the integer range") } hits <-...