Created
October 29, 2014 13:07
-
-
Save robsonbittencourt/8f3a3ccb967aa06d3984 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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