Overview
Scala is a JVM-based language that combines object-oriented and
functional programming. It interoperates with Java and is commonly used
in distributed data systems.
Apache Spark is implemented in Scala. This makes Scala a natural choice
for large-scale data processing and simulation workloads.
This article demonstrates a simple Scala program that estimates the
value of π (Pi) using a Monte Carlo simulation.
Monte Carlo Method
Monte Carlo methods estimate numerical results through random sampling.
To estimate π:
- Define a square with coordinates
[-1, 1] x [-1, 1]. - Inscribe a unit circle centered at
(0,0). - Randomly generate points inside the square.
- Count how many points fall inside the circle.
The probability of a point falling inside the circle equals the ratio of
the circle's area to the square's area.
π / 4 ≈ points_inside_circle / total_points
Therefore:
π ≈ 4 * (points_inside_circle / total_points)
Scala Implementation
Complete Program
import scala.util.Random
object PiEstimator {
def main(args: Array[String]): Unit = {
// Number of random samples
val totalPoints = 10000000
// Check if a point lies inside the unit circle
def isInCircle(x: Double, y: Double): Boolean = {
(x * x + y * y) <= 1
}
// Generate random points and count hits
val pointsInsideCircle = (1 to totalPoints).map { _ =>
val x = Random.nextDouble() * 2 - 1
val y = Random.nextDouble() * 2 - 1
if (isInCircle(x, y)) 1 else 0
}.sum
// Estimate Pi
val piEstimate = (4.0 * pointsInsideCircle) / totalPoints
println(f"Estimated Pi: $piEstimate%.5f")
}
}
Algorithm Breakdown
1. Sample Size
val totalPoints = 10000000
- Defines the number of random samples.
- Larger samples improve accuracy.
- Computational cost increases linearly.
2. Circle Membership Test
(x * x + y * y) <= 1
A point lies inside the unit circle if:
x² + y² ≤ 1
This checks whether the distance from the origin is less than or equal
to the radius.
3. Random Point Generation
val x = Random.nextDouble() * 2 - 1
val y = Random.nextDouble() * 2 - 1
- Generates coordinates in the range
[-1, 1]. - Represents uniform sampling within the square.
4. Pi Estimation
π ≈ 4 * (points_inside_circle / total_points)
The square area is 4.
The ratio between points inside the circle and total points approximates
π / 4.
Example Output
Estimated Pi: 3.14163
The estimate converges toward 3.14159 as the number of samples
increases.
Practical Considerations
Monte Carlo simulations scale well with parallel execution.
This makes them suitable for distributed systems such as Apache
Spark, where:
- Each worker generates random samples.
- Results are aggregated to compute the final estimate.
Scala integrates directly with Spark's distributed data model, allowing
this simulation to scale to billions of samples.
Summary
Key points:
- Scala runs on the JVM and integrates with Spark.
- Monte Carlo methods approximate results using random sampling.
- π can be estimated by sampling points in a square and testing circle
membership. - Accuracy increases with sample size.
- The method parallelizes efficiently in distributed systems.
Member discussion: