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

StringgetRandomString(int size)
get Random String
char[] vals = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I',
        'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Y', 'X', 'C', 'V', 'B', 'N', 'M' };
String randomString = "";
java.util.Random rand = new java.util.Random();
for (int i = 0; i < size; i++)
    randomString += vals[rand.nextInt(size)];
return randomString;
StringgetRandomString(int size)
get Random String
StringBuilder s = new StringBuilder(size);
int len = CHARS.length;
for (int i = 0; i < size; i++) {
    int x = RANDOM.nextInt();
    s.append(CHARS[(x < 0 ? -x : x) % len]);
return s.toString();
StringgetRandomString(int size)
get Random String
String randomString;
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < size; i++) {
    char c = chars[random.nextInt(chars.length)];
    sb.append(c);
randomString = sb.toString();
return randomString;
StringgetRandomString(int strLength)
get Random String
StringBuffer buffer = new StringBuffer();
Random random = new Random();
for (int i = 0; i < strLength; ++i) {
    int charInt;
    char c;
    if (random.nextBoolean()) {
        charInt = 48 + random.nextInt(10);
        c = (char) charInt;
...
StringgetRandomString(Random random, int minLen, int maxLen)
get Random String
StringBuilder s = new StringBuilder();
int length = minLen + random.nextInt(maxLen - minLen + 1);
for (int i = 0; i < length; i++) {
    s.append(getRandomChar(random, i == 0));
return s.toString();
StringgetRandomString(Random random, int totalLength, int randomSectionLength)
Naive implementation - come up with something smarter.
String randomSection = "";
int r;
if (totalLength < randomSectionLength) {
    randomSectionLength = totalLength;
if (randomSectionLength <= 0) {
    throw new IllegalArgumentException("invalid length " + randomSectionLength);
for (int i = 0; i < randomSectionLength; i++) {
    r = random.nextInt(122);
    if (r >= 0 && r <= 25) {
        r += 65;
    } else if (r >= 26 && r <= 47) {
        r += 71;
    } else if (r >= 58 && r <= 64) {
        r += 10;
    } else if (r >= 91 && r <= 96) {
        r += 10;
    randomSection += ((char) r);
if (totalLength == randomSectionLength) {
    return randomSection;
} else if (totalLength > randomSectionLength) {
    char[] src = randomSection.toCharArray();
    int sections = totalLength / randomSectionLength;
    char[] buffer = new char[totalLength];
    for (int i = 0; i < sections; i++) {
        System.arraycopy(src, 0, buffer, i * randomSectionLength, randomSectionLength);
    int rest = totalLength - sections * randomSectionLength;
    System.arraycopy(src, 0, buffer, sections * randomSectionLength, rest);
    return new String(buffer);
throw new RuntimeException("NOT YET IMPLEMENTED");
StringgetRandomString(Random rnd, int minLength, int maxLength)
Creates a random string with a length within the given interval.
int len = rnd.nextInt(maxLength - minLength + 1) + minLength;
char[] data = new char[len];
for (int i = 0; i < data.length; i++) {
    data[i] = (char) (rnd.nextInt(0x7fff) + 1);
return new String(data);
StringgetRandomString(String paramString, int paramLength)
Generate Random String
if (paramLength < 1) {
    throw new IllegalArgumentException("paramLength must be positive");
StringBuilder stringBuilder = new StringBuilder(paramLength);
Random random = new Random();
for (int j = 0; j < paramLength; j++) {
    stringBuilder.append(paramString.charAt(random.nextInt(paramString.length())));
return stringBuilder.toString();
StringgetRandomStringByLength(int length)
get Random String By Length
if (length == 0) {
    length = 32;
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
    int number = random.nextInt(base.length());
...
StringgetRandomStringForDate()
get Random String For Date
if (random == null) {
    random = new Random();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SS");
String dateStr = formatter.format(new Date());
return String.format("%s_%d", dateStr, (int) (random.nextFloat() * 1000));