Java Utililty Methods Random Int

List of utility methods to do Random Int

Description

The list of methods to do Random Int are organized into topic(s).

Method

StringrandomString(int length)
Generate a random String.
String result = "";
int j = allowableChars.length();
for (int a = 0; a < length; a++) {
    int index = new Double(Math.random() * j).intValue();
    result += allowableChars.charAt(index);
return result;
StringrandomString(int min, int max)
Returns a random string for captchas
int num = randomInt(min, max);
byte b[] = new byte[num];
for (int i = 0; i < num; i++) {
    b[i] = (byte) randomInt('a', 'z');
return new String(b);
StringrandomString(int numChar)
random String
StringBuilder s = new StringBuilder();
for (int j = 0; j < numChar; j++) {
    int nextChar = (int) (Math.random() * 62);
    if (nextChar < 10) 
        s.append(nextChar);
    } else if (nextChar < 36) 
        s.append((char) (nextChar - 10 + 'a'));
    } else 
        s.append((char) (nextChar - 36 + 'A'));
return s.toString();
StringrandomString(int size)
random String
String str = "";
for (int i = 0; i < size; ++i) {
    str = str + randomChar();
return str;
StringrandomString(int size, String... keys)
Return an random string
if (keys == null || size < 1) {
    throw new IllegalStateException("Params is wrong");
StringBuilder builder = new StringBuilder();
int length = keys.length;
for (int i = 0; i < size; i++) {
    builder.append(keys[((int) (Math.random() * length))]);
return builder.toString();
StringrandomStringNumbers(int len)
Generate a string only with digits from 0 to 9, randomically, with the length of len parameter.
String s = "";
for (int i = 0; i < len; i++)
    s = s + (int) (Math.random() * 10);
return s;
intrandomtest(int max)
randomtest
int x = 0;
if (max > 0) {
    x = (int) (Math.random() * max);
    return x;
return 0;
StringrandomText(int length)
Create a random string with upper and lower case latin letters, spaces and paragraphs.
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
    sb.append(randomChar());
return sb.toString();
intrandomWithinRange(int min, int max)
random Within Range
return (int) Math.round(Math.random() * (max - min) + min);
intrandomWithRange(int min, int max)
Method for generating random integer between the 2 parameters, The order of min and max do not matter.
int range = Math.abs(max - min) + 1;
return (int) (Math.random() * range) + (min <= max ? min : max);