-
Notifications
You must be signed in to change notification settings - Fork 9
Monte Carlo Method to Estimate Pi
Rohit edited this page Jan 23, 2017
·
2 revisions
Consider a square of side length = 2 and a circle of diameter = 2. The circle is inside the square.
Ratio between the surfaces of 1/4 of a circle and 1/4 of a square:
λ = ( pi*(1^2)/2 / (2^2)/4 )
λ = pi/4
Estimating λ: randomly sample points inside the square.
Count how many fall inside the circle. Multiply this ratio by 4 for an estimate of pi
import scala.util.Random
def mcCount(iter: Int): Int = {
val randomX = new Random
val randomY = new Random
var hits = 0
for (i <- 0 until iter) {
// since we are in the quarter of the cicle/square we get coordinates from 0 to 1
val x = randomX.nextDouble // in [0,1]
val y = randomY.nextDouble // in [0,1]
if (x*x + y*y < 1) hits= hits + 1
}
hits
}
def monteCarloPiSeq(iter: Int): Double = 4.0 * mcCount(iter) / iter
def monteCarloPiPar(iter: Int): Double = {
val ((pi1, pi2), (pi3, pi4)) = parallel( parallel(mcCount(iter/4), mcCount(iter/4)),
parallel(mcCount(iter/4), mcCount(iter - 3*(iter/4))) )
4.0 * (pi1 + pi2 + pi3 + pi4) / iter
}
Week 1
- Introduction to Parallel Computing
- Parallelism on the JVM
- Running Computations in Parallel
- Monte Carlo Method to Estimate Pi
- First Class Tasks
- How fast are parallel programs?
- Benchmarking Parallel Programs
Week 2
- Parallel Sorting
- Data Operations
- Parallel map()
- Parallel fold()
- Associativity I
- Associativity II
- Parallel Scan (Prefix Sum) Operation
Week 3
- Data Parallel Programming
- Data Parallel Operations
- Scala Parallel Collections
- Splitters and Combiners
Week 4