Link Search Menu Expand Document

Pi estimation algorithm with Monte Carlo method

Monte Carlo Pi estimation

Pi estimation in one of the main examples of Monte Carlo algorithm. The idea is to simulate random (x, y) points in a 2-D plane with domain as a square of side 1 unit and then to calculate the ratio of number points that lied inside a circle inscribed into the square and total number of generated points.

How to run

In order to correctly run the algorithm, there are some rules that must be respectes:

  • The values of the variable nthread in the environment declararion, the upper bound of the cycle on the variable i in the estimation function and the upper bound in the fly function invocation must be equal.
  • The value of nThread must be greater than 0.
  • There are multiple declaration of the cloud environment, one for each supported Cloud Provider. Only one of them could not be commented.

Functions

The program is composed by two functions:

Pi Function

The pi function can be executed locally or serverless. After generating some random values, it sends an integer on the ch channel. The value sent on ch is 1 if the generated point is into the circle, otherwise it’s 0. The Fly code is the following:

func pi(){	
   var r = [type="random"]
   var x = r.nextDouble()
   var y = r.nextDouble()
   var msg = 0
 
   if((x * x)+(y * y) < 1.0){msg = 1}
   ch!msg on cloud
}

Estimation Function

The estimation function collects the values sent on the ch channel and estimates the value of pi. The crt variable represents the number of generated points, while sum represents the number of points into the circle. The Fly code is the following:

  func estimation(){
   var sum = 0
   var crt = 0
   for i in [0:10] {
       sum += ch? as Integer
       crt += 1
   }
   println "pi estimation: " + (sum*4.0) / crt
}