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

intrandomIntInRange(int lowerBound, int upperBound)
random Int In Range
if (upperBound < lowerBound)
    throw new IllegalArgumentException("lowerBound must be smaller than upperBound");
if (upperBound == lowerBound)
    return upperBound;
double rand = Math.random();
rand = rand * (upperBound - lowerBound);
rand += lowerBound;
float r = (float) rand;
...
intrandomIntRange(int low, int high)
Get a random integer in the given half-open range.
return low + ((int) Math.floor(Math.random() * (high - low)));
intrandomIntWithDigits(int digits)
random Int With Digits
int max = (int) Math.pow(10, digits) + 1;
return randomIntWithMinMax(0, max);
StringrandomKey(int sLen)
random Key
String base;
String temp;
int i;
int p;
base = "1234567890abcdefghijklmnopqrstuvwxyz";
temp = "";
for (i = 0; i < sLen; i++) {
    p = (int) (Math.random() * 37);
...
int[][]randomMatrix(int x, int y)
random Matrix
int[][] matrix = new int[x][y];
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[0].length; j++) {
        matrix[i][j] = (int) (Math.random() * 10);
return matrix;
StringrandomModuleName(int count)
Generate random module name.
String upperCaseAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerCaseAlpha = "abcdefghijklmnopqrstuvwxyz";
String alpha = upperCaseAlpha + lowerCaseAlpha;
StringBuilder builder = new StringBuilder();
while (count-- != 0) {
    int character = (int) (Math.random() * alpha.length());
    builder.append(alpha.charAt(character));
return builder.toString();
intrandomNum(int n)
random Num
return (int) (Math.random() * Math.pow(10, n));
StringrandomNum(int number)
random Num
StringBuffer result = new StringBuffer();
for (int i = 0; i < number; i++) {
    int random = (int) (Math.random() * 10);
    result.append(random);
return result.toString();
intrandomNumber(int lower, int upper)
random Number
return lower + (int) (Math.random() * ((upper - lower) + 1));
intrandomNumber(int lower_bound, int higher_bound)
Random number.
int random_number = lower_bound + (int) (Math.random() * ((higher_bound - lower_bound) + 1));
return random_number;