Created
April 12, 2010 12:26
-
-
Save tbje/363500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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