Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Last active March 15, 2020 15:26
Show Gist options
  • Save jakubkulhan/9b408a9a1d2683f9ee23303c36ba23ad to your computer and use it in GitHub Desktop.
Save jakubkulhan/9b408a9a1d2683f9ee23303c36ba23ad to your computer and use it in GitHub Desktop.
import java.security.SecureRandom;
public class RandomString {
private static final String ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
private static final SecureRandom RANDOM = new SecureRandom();
/**
* Generates random string of given length from Base65 alphabet (numbers, lowercase letters, uppercase letters).
*
* @param count length
* @return random string of given length
*/
public static String generate(int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; ++i) {
sb.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length())));
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment