Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Last active August 29, 2015 13:56
Show Gist options
  • Save kamiyaowl/9049540 to your computer and use it in GitHub Desktop.
Save kamiyaowl/9049540 to your computer and use it in GitHub Desktop.
[scala]無限リストを用いたモンテカルロ法比較
package scalatan
import util.Random
import math._
object Main {
def main(args: Array[String]): Unit = {
def create() : Stream[Double] = {
Stream.cons(Random.nextDouble(),create())
}
val n = 1000
val hit = create() zip create() take n filter(x => x._1 * x._1 + x._2 * x._2 <= 1.0 )
print(4.0 * hit.length / n)
}
}
package scalatan
import util.Random
import math._
object Main {
def main(args: Array[String]): Unit = {
val n = 1000
var hit = 0
for(i <- 0 until n){
val x = Random.nextDouble()
val y = Random.nextDouble()
if(x * x + y * y <= 1.0) hit +=1
}
print(4.0 * hit / n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment