Quick actions

cmd+k|ctrl+k

Navigation

Languages

Coin throw binomial distribution histogram

Snippet info

Language

Scala

Visibility

public

Author

cavalheiro

Created

2016-06-23T17:25:30Z

Updated

2016-06-23T17:26:47Z

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")
	})
}
INFO