Last active
November 3, 2017 23:43
-
-
Save krishnanraman/5d399626895c129f7235b768ee2456e6 to your computer and use it in GitHub Desktop.
taleb dart problem
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
$ scala TalebDart | |
Exactly Three | |
0.1584 | |
Atleast Three | |
0.1726 | |
Atleast Three no doublecount! | |
0.1693 |
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
import util.Random | |
object TalebDart extends App { | |
case class Dart(x:Double, y:Double) | |
case class Box(minx:Double, miny:Double) { | |
var count = 0 | |
def inBox(dart:Dart):Boolean = { | |
val isin = dart.x >= minx && dart.x <= minx + 0.25 && | |
dart.y >= miny && dart.y <= miny + 0.25 | |
if (isin) count += 1 | |
isin | |
} | |
def hasThreeDarts:Int = if (count == 3) 1 else 0 | |
def atleastThreeDarts:Int = if (count >= 3) 1 else 0 | |
def reset = count = 0 | |
} | |
val simulations = 1000*1000*10 | |
val rnd = new Random() | |
var count = 0 | |
val boxes:List[Box] = List( | |
Box(0,0), Box(0.25,0), Box(0.5,0), Box(0.75,0), | |
Box(0,0.25), Box(0.25,0.25), Box(0.5,0.25), Box(0.75,0.25), | |
Box(0,0.5), Box(0.25,0.5), Box(0.5,0.5), Box(0.75,0.5), | |
Box(0,0.75), Box(0.25,0.75), Box(0.5,0.75), Box(0.75,0.75) | |
) | |
////////////////////////////////////////////////////////////////// | |
count = 0 | |
println("Exactly Three") | |
(1 to simulations).foreach { iter => | |
val darts = (1 to 8).map{ n => | |
Dart(rnd.nextDouble, rnd.nextDouble) | |
} | |
boxes.foreach{ _.reset } | |
boxes.foreach{ box => darts.foreach{ dart => box.inBox(dart)}} | |
count += boxes.map{ box => box.hasThreeDarts }.sum | |
} | |
printf("%.4f\n", count.toDouble/simulations) | |
/////////////////////////////////////////////////////////////////// | |
count = 0 | |
println("Atleast Three") | |
(1 to simulations).foreach { iter => | |
val darts = (1 to 8).map{ n => | |
Dart(rnd.nextDouble, rnd.nextDouble) | |
} | |
boxes.foreach{ _.reset } | |
boxes.foreach{ box => darts.foreach{ dart => box.inBox(dart)}} | |
count += boxes.map{ box => box.atleastThreeDarts }.sum | |
} | |
printf("%.4f\n", count.toDouble/simulations) | |
/////////////////////////////////////////////////////////////////// | |
count = 0 | |
println("Atleast Three no doublecount!") | |
(1 to simulations).foreach { iter => | |
val darts = (1 to 8).map{ n => | |
Dart(rnd.nextDouble, rnd.nextDouble) | |
} | |
boxes.foreach{ _.reset } | |
boxes.foreach{ box => darts.foreach{ dart => box.inBox(dart)}} | |
if (boxes.map{ box => box.atleastThreeDarts }.sum > 0) | |
count += 1 | |
} | |
printf("%.4f\n", count.toDouble/simulations) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment