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

Stringgetrandomstr()
getrandomstr
StringBuffer sb = new StringBuffer();
int n = r.nextInt(12);
for (int i = 0; i < n; i++) {
    sb.append((char) (r.nextInt(26) + 'a'));
return sb.toString();
StringgetRandomStr()
get Random Str
StringBuilder sb = new StringBuilder();
sb.append(System.currentTimeMillis());
sb.append(new Random().nextInt(10000));
return sb.toString();
StringgetRandomStr(int Len)
get Random Str
String[] baseString = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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" };
Random random = new Random();
int length = baseString.length;
String randomString = "";
for (int i = 0; i < length; i++) {
...
StringgetRandomStr(int length)
get Random Str
Random randGen = null;
char[] numbersAndLetters = null;
Object initLock = new Object();
if (length < 1) {
    return null;
if (randGen == null) {
    synchronized (initLock) {
...
StringGetRandomStr(int length)
Get Random Str
Random rnd = new Random(System.currentTimeMillis());
StringBuffer buf = new StringBuffer();
for (int i = 0; i < length; i++) {
    int val = rnd.nextInt(chars.length());
    buf.append(chars.charAt(val));
return buf.toString();
StringgetRandomStr(int length)
get Random Str
StringBuffer sbf = new StringBuffer();
Random rand = new Random();
int charSize = characters.length();
for (int i = 0; i < length; i++) {
    sbf.append(characters.charAt(rand.nextInt(charSize)));
return sbf.toString();
StringgetRandomStr(int n)
get Random Str
String s = "";
Random random = new Random();
for (int i = 0; i < n; i++) {
    s = s + random.nextInt(10);
return s;
StringgetRandomStr(int qty)
get Random Str
String d = "abcdfghijklmnopqrtuvwxyz";
Random rand = new Random();
int i = 0;
int n = 0;
String f = "";
while (i < qty) {
    n = rand.nextInt(24 - 0 + 1) + 0;
    f += d.substring(n, n + 1);
...
StringgetRandomStr(String src)
get Random Str
if (null == src || "".equals(src)) {
    return "";
Random random = new Random();
return String.valueOf(src.charAt(random.nextInt(src.length())));
StringgetRandomString()
get Random String
return bytes2Hex(getRandomBytes());