ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Julia Monte Carlo Integration: Pi Estimation

Julia

Goal -- WPM

Ready
Exercise Algorithm Area
1using Random
2
3function estimate_pi_monte_carlo(num_samples::Int)
4if num_samples <= 0
5error("Number of samples must be positive.")
6end
7
8points_inside_circle = 0
9total_points = num_samples
10
11# We consider a square with corners at (-1,-1) and (1,1), and a circle inscribed within it.
12# The area of the square is (2*1)^2 = 4.
13# The area of the circle is pi * r^2 = pi * 1^2 = pi.
14# The ratio of circle area to square area is pi / 4.
15
16for _ in 1:num_samples
17# Generate random x and y coordinates between -1 and 1
18x = rand() * 2.0 - 1.0
19y = rand() * 2.0 - 1.0
20
21# Check if the point (x, y) is inside the unit circle (x^2 + y^2 <= 1)
22distance_squared = x^2 + y^2
23if distance_squared <= 1.0
24points_inside_circle += 1
25end
26end
27
28# The ratio of points inside the circle to total points approximates the ratio of areas.
29# (points_inside_circle / total_points) (pi / 4)
30# Therefore, pi 4 * (points_inside_circle / total_points)
31estimated_pi = 4.0 * (points_inside_circle / total_points)
32
33return estimated_pi
34end
35
36# Example Usage:
37# num_samples = 1000000
38# pi_estimate = estimate_pi_monte_carlo(num_samples)
39# println("Estimated value of Pi with ", num_samples, " samples: ", pi_estimate)
Algorithm description viewbox

Julia Monte Carlo Integration: Pi Estimation

Algorithm description:

This Julia function estimates the value of Pi using a Monte Carlo simulation. It generates a large number of random points within a square that inscribes a circle. By counting how many points fall inside the circle, it approximates the ratio of the circle's area to the square's area, which is directly related to Pi. This method is a classic example of using randomness to solve deterministic problems and is useful in various scientific and statistical applications.

Algorithm explanation:

The `estimate_pi_monte_carlo` function leverages the Monte Carlo method. It simulates throwing darts randomly at a square target containing an inscribed circle. The probability of a dart landing inside the circle is the ratio of the circle's area (πr²) to the square's area ((2r)²), which simplifies to π/4. By generating `num_samples` random points (x, y) within the square [-1, 1] x [-1, 1] and checking if `x^2 + y^2 <= 1`, we count how many fall inside the unit circle. The estimated value of Pi is then `4 * (points_inside_circle / num_samples)`. The time complexity is O(num_samples) because each sample requires constant time operations. Space complexity is O(1) as it only stores counters. Edge cases include non-positive sample counts, which are handled by an error.

Pseudocode:

function estimate_pi_monte_carlo(num_samples):
  if num_samples <= 0: error

  points_inside_circle = 0
  total_points = num_samples

  for i from 1 to num_samples:
    generate random x between -1 and 1
    generate random y between -1 and 1

    distance_squared = x^2 + y^2
    if distance_squared <= 1.0:
      increment points_inside_circle

  estimated_pi = 4.0 * (points_inside_circle / total_points)
  return estimated_pi