Skip to content

Instantly share code, notes, and snippets.

@r-wheeler
Created January 11, 2015 07:03
Show Gist options
  • Save r-wheeler/fd8752bf7531b6585cf3 to your computer and use it in GitHub Desktop.
Save r-wheeler/fd8752bf7531b6585cf3 to your computer and use it in GitHub Desktop.
DailyProgrammer[7/13/2012]
//print a histogram
import scala.util.Random
import scala.annotation.tailrec
def twoDice(): Int = {
val r1 = Random
(r1.nextInt(6)+1) + (r1.nextInt(6) +1)
}
def graph(f: => Int, low: Int, high: Int, tests: Int) = {
@tailrec
def go(l: List[Int], n: Int): List[Int] =
if( n >= tests) l else go(f :: l, n +1)
def scale(n: Int): Int = (n.toDouble / tests.toDouble * 100.0).toInt
val samples = go(Nil,0)
samples.groupBy(identity)
.mapValues(_.size)
.toList
.sortBy(_._1)
.foreach {
case (k, v) =>
val hist = "#" * scale(v)
println(s"$k $hist \n")
}
}
//graph(twoDice, 0,0,1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment