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 length)
get Random String
char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
return getRandomString(length, chars);
StringgetRandomString(int length)
get Random String
String RandomCode = "";
char[] codeSequence = { '3', '4', '5', '6', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6',
        '7', '8', '9' };
Random random = new Random();
StringBuffer randomCode = new StringBuffer();
for (int i = 0; i < length; i++) {
    String strRand = String.valueOf(codeSequence[random.nextInt(40)]);
...
StringgetRandomString(int length, Random rnd)
get Random String
String s = Long.toHexString(Double.doubleToLongBits(rnd.nextDouble()));
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < s.length() && i < length; i++) {
    strBuilder.append(s.charAt(Math.abs(rnd.nextInt()) % s.length()));
return strBuilder.toString();
StringgetRandomString(int length, String charset)
Generate a random string using current time and charset
Random rand = new Random(System.currentTimeMillis());
StringBuilder sb = new StringBuilder();
if (charset == null) {
    return sb.toString();
for (int i = 0; i < length; i++) {
    int pos = rand.nextInt(charset.length());
    sb.append(charset.charAt(pos));
...
StringgetRandomString(int min, int max)
get Random String
String newstring = new String();
Random rand = new Random();
int i;
final char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
        'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
        'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@', '#', '$', '%',
        '^', '&', '*', '(', ')', '_', '-', '=', '+', '{', '}', '[', ']', '|', ':', ';', ',', '.', '?', '/',
        '~', ' ' }; 
...
StringgetRandomString(int minLength, int maxLength)
get Random String
int length;
if (minLength == maxLength) {
    length = minLength;
} else {
    length = RANDOM.nextInt(maxLength - minLength) + minLength;
char[] randomString = new char[length];
for (int x = 0; x < length; x++) {
...
StringgetRandomString(int randomPasswordLength)
Generate random string for password
return getRandomString(randomPasswordLength, RANDOM_PATTERN);
StringgetRandomString(int size)
get Random String
Random random = new Random();
StringBuffer sb = new StringBuffer(size);
for (int i = 0; i < size; i++) {
    sb.append(c[Math.abs(random.nextInt(CHART_LENGHT))]);
return sb.toString();
StringgetRandomString(int size)
get Random String
Random random = new Random();
StringBuffer sb = new StringBuffer(size);
for (int i = 0; i < size; i++) {
    sb.append(c[Math.abs(random.nextInt()) % c.length]);
return sb.toString();
StringgetRandomString(int size)
Generates a random String of length size using the default encoding scheme.
StringBuilder builder = new StringBuilder(size);
for (int i = 0; i < size; i++) {
    char c = (char) (random.nextInt(126 - 33) + 33);
    builder.append(c);
return builder.toString();