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 len, boolean ascii_only)
get Random String
Random r = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i++) {
    char c;
    if (ascii_only) {
        c = (char) r.nextInt(62);
        if (c < 10) {
            c = (char) (48 + c);
...
StringgetRandomString(int lenght)
get Random String
Random rnd = new Random(System.currentTimeMillis());
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder b = new StringBuilder();
for (int i = 0; i < lenght; i++) {
    b.append(alpha.charAt(rnd.nextInt(alpha.length())));
return b.toString();
StringgetRandomString(int length)
get Random String
if (length <= 0) {
    return "";
char[] randomChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'q', 'w', 'e', 'r', 't', 'y', 'u',
        'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
Random random = new Random();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < length; i++) {
...
StringgetRandomString(int length)
get Random String
Random random = new Random();
StringBuffer sb = new StringBuffer();
if (length < 1) {
    length = 1;
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (int i = 0; i < length; i++) {
    int number = random.nextInt(base.length());
...
StringgetRandomString(int length)
Generates a random name, generated with java.util.Random and an Alphabet of 0-9,a-z,A-Z
e.g.
char[] values = { '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', '0', '1', '2', '3', '4', '5',
        '6', '7', '8', '9' };
Random random = new Random();
StringBuffer stringBuf = new StringBuffer();
for (int i = 0; i < length; i++) {
    stringBuf.append(values[Math.abs(random.nextInt()) % (values.length)]);
...
StringgetRandomString(int length)
get the alphanumeric string with the specific length.
if (length <= 0)
    length = 8;
byte[] bytes = new byte[length];
random.nextBytes(bytes);
StringBuffer sb = new StringBuffer(length);
for (int i = 0; i < length; i++) {
    sb.append(m_alphanumeric[Math.abs(bytes[i] % 36)]);
return sb.toString();
StringgetRandomString(int length)
get Random String
char[] text = new char[length];
for (int i = 0; i < length; i++) {
    text[i] = CHARACTERS.charAt(random.nextInt(CHARACTERS.length()));
return new String(text);
StringgetRandomString(int length)
get Random String
char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
Random random = new Random();
StringBuilder stringBuiler = new StringBuilder();
for (int i = 0; i < length; i++) {
    stringBuiler.append(chars[Math.abs(random.nextInt()) % 16]);
return stringBuiler.toString();
StringgetRandomString(int length)
get Random String
char[] charaters = { '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' };
return getRandomString(length, charaters);
StringgetRandomString(int length)
get Random String
if (rand == null) {
    rand = new Random(System.currentTimeMillis());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
    int pos = rand.nextInt(charset.length());
    sb.append(charset.charAt(pos));
return sb.toString();