Last active
March 15, 2020 15:26
-
-
Save jakubkulhan/9b408a9a1d2683f9ee23303c36ba23ad 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
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