RosettaCodeData/Task/Monte-Carlo-methods/Scala/monte-carlo-methods.scala
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

27 lines
949 B
Scala

object MonteCarlo {
private val random = new scala.util.Random
/** Returns a random number between -1 and 1 */
def nextThrow: Double = (random.nextDouble * 2.0) - 1.0
/** Returns true if the argument point would be 'inside' the unit circle with
* center at the origin, and bounded by a square with side lengths of 2
* units. */
def insideCircle(pt: (Double, Double)): Boolean = pt match {
case (x, y) => (x * x) + (y * y) <= 1.0
}
/** Runs the simulation the specified number of times. Uses the result to
* estimate a value of pi */
def simulate(times: Int): Double = {
val inside = Iterator.tabulate (times) (_ => (nextThrow, nextThrow)) count insideCircle
inside.toDouble / times.toDouble * 4.0
}
def main(args: Array[String]): Unit = {
val sims = Seq(10000, 100000, 1000000, 10000000, 100000000)
sims.foreach { n =>
println(n+" simulations; pi estimation: "+ simulate(n))
}
}
}