Skip to content

Instantly share code, notes, and snippets.

@rafayali
Created April 1, 2015 12:17
Show Gist options
  • Save rafayali/17d6692ae48d0f616c86 to your computer and use it in GitHub Desktop.
Save rafayali/17d6692ae48d0f616c86 to your computer and use it in GitHub Desktop.
Random string unique hash generator
private static char[] VALID_CHARACTERS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456879".toCharArray();
public static String csRandomAlphaNumericString(int numChars) throws Exception{
SecureRandom srand = new SecureRandom();
Random rand = new Random();
char[] buff = new char[numChars];
for (int i = 0; i < numChars; ++i) {
if ((i % 10) == 0) {
rand.setSeed(srand.nextLong());
}
buff[i] = VALID_CHARACTERS[rand.nextInt(VALID_CHARACTERS.length)];
}
return new String(buff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment