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

StringrandomString(int length)
random String
if (length < 1) {
    return null;
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
    randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
return new String(randBuffer);
...
StringrandomString(int length)
Generates a random string of length length, containing a-z, A-Z, 0-9
return getRandomString(length);
StringrandomString(int length)
Returns a random String of numbers and letters of the specified length.
if (length < 1) {
    return null;
if (randGen == null) {
    synchronized (initLock) {
        if (randGen == null) {
            randGen = new Random();
            numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
...
StringrandomString(int length)
Returns a random String of numbers and letters (lower and upper case) of the specified length.
if (length < 1) {
    return null;
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
    randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
return new String(randBuffer);
...
Stringrandomstring(int lo, int hi)
randomstring
int n = rand(lo, hi);
byte b[] = new byte[n];
for (int i = 0; i < n; i++)
    b[i] = (byte) rand('a', 'z');
return new String(b, 0);
StringrandomString(int stringLength)
random String
StringBuffer s = new StringBuffer();
for (int i = 0; i < stringLength; i++) {
    int c = rand.nextInt(26);
    s.append(alphabet.charAt(c));
return s.toString();
StringrandomString(Integer len)
Generates random alpha-numeric string of specified length
final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random rnd = new Random();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
    sb.append(AB.charAt(rnd.nextInt(AB.length())));
return sb.toString();
StringrandomString(Random r, int minCount, int maxCount)
random String
minCount = Math.max(Math.min(minCount, maxCount), 1);
maxCount = Math.max(minCount, maxCount);
final int stringCount = minCount + r.nextInt(maxCount - minCount);
char[] s = new char[stringCount];
for (int i = 0; i < stringCount; ++i) {
    s[i] = (char) (32 + r.nextInt(127 - 32));
return new String(s);
...
String[]randomSubset(String[] list, int count)
Returns a subset of the list supplied.
if (list.length < count)
    return null;
Vector vList = new Vector();
for (int i = 0; i < list.length; i++) {
    vList.addElement(list[i]);
Random random = new Random(System.currentTimeMillis());
String RandomItems[] = new String[count];
...
StringrandomSuffix(final String name)
random Suffix
return name + "-" + System.currentTimeMillis();