Last active
August 29, 2015 13:56
-
-
Save kamiyaowl/9049540 to your computer and use it in GitHub Desktop.
[scala]無限リストを用いたモンテカルロ法比較
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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