Skip to content

Instantly share code, notes, and snippets.

@tbje
Created April 12, 2010 12:26
Show Gist options
  • Save tbje/363500 to your computer and use it in GitHub Desktop.
Save tbje/363500 to your computer and use it in GitHub Desktop.
/* http://tbje.blogspot.com/2010/04/random-unique-hash-in-scala.html */
import scala.util.Random
def uniqueRandomKey(chars: String, length: Int, uniqueFunc: String=>Boolean) : String = {
val newKey = (1 to length).map(x => chars(Random.nextInt(chars.length))).mkString
if (uniqueFunc(newKey)) newKey else uniqueRandomKey(chars, length, uniqueFunc)
}
val chars = ('a' to 'z') ++ ('A' to 'Z') ++ ('1' to '9')
val key = uniqueRandomKey(chars.toString, 22, (x:String)=>!keyExists?(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment