Java Utililty Methods Random String

List of utility methods to do Random String

Description

The list of methods to do Random String are organized into topic(s).

Method

StringgenerateRandomString(int length)
generate Random String
char[] characters = new char[length];
for (int i = 0; i < length; i++) {
    char start, end;
    switch (random.nextInt(4)) {
    case 0:
        start = 'A';
        end = 'Z';
    case 1:
...
StringgenerateRandomString(int length)
Generate a random character string

String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int N = alphabet.length();
Random r = new Random();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
    sb.append(alphabet.charAt(r.nextInt(N)));
return sb.toString();
...
StringgenerateRandomString(int length)
generate Random String
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
    int number = random.nextInt(base.length());
    sb.append(base.charAt(number));
return sb.toString();
...
StringgenerateRandomString(int length)
generate Random String
Random rngRandom = new Random();
char[] text = new char[length];
int lengRnd = RANDOM_STRING.length();
for (int i = 0; i < length; i++) {
    text[i] = RANDOM_STRING.charAt(rngRandom.nextInt(lengRnd));
return new String(text);
StringgenerateRandomString(int Length, String caseType)
generate Random String
String[] caseList = new String[Length];
if (caseType.toUpperCase().matches("UPPER")) {
    for (int i = 0; i < caseList.length; i++) {
        caseList[i] = "UPPER";
} else if (caseType.toUpperCase().matches("LOWER")) {
    for (int i = 0; i < caseList.length; i++) {
        caseList[i] = "LOWER";
...
StringgenerateRandomString(int length, String charset)
Generate a random n-length alphanumeric string.
String str;
try {
    str = new String(new byte[] {}, charset);
} catch (UnsupportedEncodingException ex) {
    System.err.println("ERROR: Unsupported encoding: " + charset);
    ex.printStackTrace();
    return null;
for (int i = 0; i < length; i++) {
    int rand = (int) (Math.random() * 62);
    if (rand > 9 && rand < 36) {
        rand += 7;
    } else if (rand > 35) {
        rand += 13;
    str += (char) (rand + 48);
return str;
StringgenerateRandomString(int minLength, int maxLength, int minLCaseCount, int minUCaseCount, int minNumCount, int minSpecialCount)
Generate a random string (with some given constraints) that can be used as (simple) password generator This code is copied from http://stackoverflow.com/questions/4090021/need-a-secure-password-generator-recommendation.
char[] randomString;
String LCaseChars = "abcdefgijkmnopqrstwxyz";
String UCaseChars = "ABCDEFGHJKLMNPQRSTWXYZ";
String NumericChars = "23456789";
String SpecialChars = "*$-+?_&=!%{}/";
Map<String, Integer> charGroupsUsed = new HashMap<>();
charGroupsUsed.put("lcase", minLCaseCount);
charGroupsUsed.put("ucase", minUCaseCount);
...
StringgenerateRandomString(int n)
generate Random String
String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int length = alphabet.length();
String s = "";
Random r = new Random(System.currentTimeMillis());
for (int i = 0; i < n; i++)
    s += alphabet.charAt(r.nextInt(length));
return s;
StringgenerateRandomString(int n)
generate Random String
Random rd = new Random();
char lowerChars[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
char upperChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
char numbers[] = "0123456789".toCharArray();
char specialChars[] = "!$-_+".toCharArray(); 
List<Character> pwdLst = new ArrayList<>();
for (int g = 0; g <= n; g++) {
    for (int z = 0; z < 1; z++) {
...
StringgenerateRandomString(int size)
generate Random String
String random = new String();
Random rand = new Random();
for (int i = 0; i < size; i++) {
    int random_nbr = rand.nextInt(random_str.length());
    random += (rand.nextBoolean()) ? random_str.charAt(random_nbr)
            : Character.toLowerCase(random.charAt(random_nbr));
return random;
...