Pi Estimation Calculation

View previous topic View next topic Go down

Pi Estimation Calculation

Post by Cr6 on Wed Feb 03, 2016 1:27 am

Pi Estimation

Spark can also be used for compute-intensive tasks. This code estimates π by "throwing darts" at a circle. We pick random points in the unit square ((0, 0) to (1,1)) and see how many fall in the unit circle. The fraction should be π / 4, so we use this to get our estimate.

val count = sc.parallelize(1 to NUM_SAMPLES).map{i =>
val x = Math.random()
val y = Math.random()
if (x*x + y*y < 1) 1 else 0
}.reduce(_ + _)
println("Pi is roughly " + 4.0 * count / NUM_SAMPLES)

https://spark.apache.org/examples.html


Cr6
Admin

Posts : 700
Join date : 2014-08-09

View user profile http://milesmathis.the-talk.net

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum