4 people like it.

# Calculate PI using Monte Carlo

Simple walkthrough that demonstrates how to estimate the value of PI using Monte Carlo simulation. A few holes need to be filled in and then you can run & parallelize the sample!

 ``` 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: ``` ``````open System /// Calculates whether a point is within the unit circle let insideCircle (x:float, y:float) = // TASK #1: Implement the test! false /// Generates random X, Y values between 0 and 1 let randomPoints max = seq { let rnd = new Random() for i in 0 .. max do yield rnd.NextDouble(), rnd.NextDouble() } /// Generate specified number of random points and /// calculate PI using Monte Carlo simulation let monteCarloPI size = // TASK #2: Generate specified number of random // points and test how many are inside circle // (...) let inside = 0 // Estimate the value of PI float inside / float size * 4.0 // Test the Monte Carlo PI calculation #time monteCarloPI 1000000 // Run the calculation 10 times and calculate average // Change to 'Array.Parallel.map' to parallelize! [| for i in 0 .. 10 -> 1000000 |] |> Array.map monteCarloPI |> Array.average ``````
namespace System
val insideCircle : x:float * y:float -> bool

Full name: Script.insideCircle

Calculates whether a point is within the unit circle
val x : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val y : float
val randomPoints : max:int -> seq<float * float>

Full name: Script.randomPoints

Generates random X, Y values between 0 and 1
val max : int
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val rnd : Random
Multiple items
type Random =
new : unit -> Random + 1 overload
member Next : unit -> int + 2 overloads
member NextBytes : buffer:byte[] -> unit
member NextDouble : unit -> float

Full name: System.Random

--------------------
Random() : unit
Random(Seed: int) : unit
val i : int
Random.NextDouble() : float
val monteCarloPI : size:int -> float

Full name: Script.monteCarloPI

Generate specified number of random points and
calculate PI using Monte Carlo simulation
val size : int
val inside : int
type Array =
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...

Full name: System.Array
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val average : array:'T [] -> 'T (requires member ( + ) and member DivideByInt and member get_Zero)

Full name: Microsoft.FSharp.Collections.Array.average