Pi Estimation Calculation

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)



Posts : 960
Join date : 2014-08-09

View user profile http://milesmathis.forumotion.com

Back to top Go down

Back to top

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