//Monte Carlo simulation
function estimatePiMonteCarlo(iterations) {
  let insideCircle = 0;

  for (let i = 0; i < iterations; i++) {
    const x = Math.random(); // Random x-coordinate between 0 and 1
    const y = Math.random(); // Random y-coordinate between 0 and 1

    // Check if the point (x, y) is inside the unit circle (radius = 1)
    if (x * x + y * y <= 1) {
      insideCircle++;
    }
  }

  // Estimate pi using the ratio of points inside the circle to total points
  const piEstimation = (insideCircle / iterations) * 4;

  return piEstimation;
}

const iterations = 1000000; // You can adjust the number of iterations for accuracy
const estimatedPi = estimatePiMonteCarlo(iterations);
console.log(`Estimated value of π with ${iterations} iterations: ${estimatedPi}`);

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: