Skip to content

Instantly share code, notes, and snippets.

@landon9720
Created August 17, 2011 19:52
Show Gist options
  • Save landon9720/1152439 to your computer and use it in GitHub Desktop.
Save landon9720/1152439 to your computer and use it in GitHub Desktop.
rnd7
import Math._
// random number in range 1 - 5
def rnd5 = (floor(random * 5) + 1).toLong
// random bit
def rndbit: Long = {
val i = rnd5
if (i == 1 || i == 2) 0L
else if (i == 3 || i == 4) 1L
else rndbit
}
// random int
def rndint = {
var i = 0l
for (shift <- 0 until 32) {
i = i | (rndbit << pow(2, shift).toLong)
}
i
}
def ran7 = (rndint % 7) + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment