Skip to content

Instantly share code, notes, and snippets.

@robsonbittencourt
Created October 29, 2014 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsonbittencourt/8f3a3ccb967aa06d3984 to your computer and use it in GitHub Desktop.
Save robsonbittencourt/8f3a3ccb967aa06d3984 to your computer and use it in GitHub Desktop.
public static Long getRandomLong() {
return Long.valueOf(new Random().nextInt(999999999) + 1);
}
public static String getRandomString() {
return getRandomString(10);
}
public static String getRandomString(int length) {
final Random random = new Random();
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
switch (random.nextInt(3)) {
case 0:
// 48 = 0, 57 = 9
sb.append(new Character((char) (48 + random.nextInt((57 + 1 - 48)))));
break;
case 1:
// 97 = a, 122 = z
sb.append(new Character((char) (97 + random.nextInt((122 + 1 - 97)))));
break;
case 2:
// 65 = A, 90 = Z
sb.append(new Character((char) (65 + random.nextInt((90 + 1 - 65)))));
break;
default:
break;
}
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment