Skip to content

Instantly share code, notes, and snippets.

@edeustace
Forked from tbje/uniqueHash.scala
Created March 30, 2012 20:52
Show Gist options
  • Save edeustace/2254888 to your computer and use it in GitHub Desktop.
Save edeustace/2254888 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 =>
{
val index = Random.nextInt(chars.length)
chars(index)
}
).mkString("")
if (uniqueFunc(newKey))
newKey
else
uniqueRandomKey(chars, length, uniqueFunc)
}
/**
* implement your own is unique here
*/
def isUnique(s:String):Boolean = true
val chars = ('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9') ++ ("-!£$")
val key = uniqueRandomKey(chars.mkString(""), 8, isUnique)
println(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment