Coin throw binomial distribution histogram

Run Settings
LanguageScala
Language Version
Run Command
object Main extends App { val trials = 1000 val throws = 10 val maxBarSize = 50 val dist = 0 :: (for (i <- 1 to trials) yield { val nh = Seq.fill(throws)(scala.util.Random.nextInt(2)) val ab = nh.sum for (i <- 0 to throws) yield if (ab == i) 1 else 0 }).transpose.map(_.sum).toList val max = dist.max dist.foreach(n => { val size = (n*maxBarSize/max) val prob = n / trials.toDouble if (size==0) print(".") else print("x"*size) println(s" $prob") }) }
Editor Settings
Theme
Key bindings
Full width
Lines