Skip to content

Instantly share code, notes, and snippets.

@drostron
Last active December 11, 2015 18:08
Show Gist options
  • Select an option

  • Save drostron/4639263 to your computer and use it in GitHub Desktop.

Select an option

Save drostron/4639263 to your computer and use it in GitHub Desktop.
Random Generator Projection Exploration
import util.Random
object RandomGeneratorProjection {
// in : size of the input generator
// out : size of the output generator
def d(in: Int, out: Int): Int = {
assert(in > 1, "in must be greater than 1")
assert(out > 0, "out must be greater than 0")
List.fill(out)(Random.nextInt(in)).zipWithIndex.sorted.reverse match {
case (a,_) :: (b,_) :: _ if a == b => d(in, out)
case (_,i) :: _ => i
}
}
def verify(in: Int, out: Int, trials: Int) =
(0 until trials).map { i => d(in, out) }.groupBy { i => i }.map { case (k,v) => k -> v.size }
// an approximate verification visual
def printVerifyViz(i: Map[Int, Int]) {
val z = i.head._2 / 80
i.toList.sorted.foreach { case (k,v) => println(s"k: $k ${"."*(v/z)}") }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment