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(final String pattern)
generate Random String
if ((pattern == null) || (pattern.length() == 0)) {
    return "";
final char[] patternChars = pattern.toCharArray();
final StringBuilder retVal = new StringBuilder(pattern.length());
final java.util.Random rndGen = new java.util.Random();
for (char patternChar : patternChars) {
    if (patternChar == '#') {
...
StringgenerateRandomString(int count)
Generate a random string.
Random random = new Random();
StringBuffer buffer = new StringBuffer();
while (count-- != 0) {
    char ch = (char) (random.nextInt(96) + 32);
    buffer.append(ch);
return buffer.toString();
StringgenerateRandomString(int len)
generate Random String
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
    sb.append(AB.charAt(random.nextInt(AB.length())));
return sb.toString();
StringgenerateRandomString(int length)
generate Random String
char[] text = new char[length];
for (int i = 0; i < length; i++) {
    text[i] = CHARACTERS[RANDOM.nextInt(CHARACTERS.length)];
return new String(text);
StringgenerateRandomString(int length)
generate Random String
Random random = new Random();
if (length < 1) {
    throw new IllegalArgumentException("length < 1: " + length);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
    int rnd = random.nextInt(10 + 'z' - 'a');
    if (rnd < 10) {
...
StringgenerateRandomString(int length)
Generates one random string
int rnd;
final Random r = new Random();
final String sorigen = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz";
final char fuente[] = sorigen.toCharArray();
char buffer[] = new char[length];
for (int i = 0; i < length; i++) {
    rnd = Math.abs(r.nextInt()) % fuente.length;
    buffer[i] = fuente[rnd];
...
StringgenerateRandomString(int length)
generate Random String
StringBuilder str = new StringBuilder();
String base = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random rnd = new Random();
for (int i = 0; i < length; i++) {
    str.append(base.charAt(rnd.nextInt(base.length())));
return str.toString();
StringgenerateRandomString(int length)
generate Random String
String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int charLength = chars.length();
StringBuilder pass = new StringBuilder(charLength);
for (int x = 0; x < length; x++) {
    int i = (int) (Math.random() * charLength);
    pass.append(chars.charAt(i));
return pass.toString();
...
StringgenerateRandomString(int length)
Generate random chars string of given length.
int blen = ((length + 3) / 4) * 3; 
byte[] bval = new byte[blen];
rnd.nextBytes(bval);
return new String(Base64.getEncoder().encode(bval), 0, length).replace('/', '$');
StringgenerateRandomString(int length)
generate Random String
return generateRandomString(RANDOM_CHARS, length);