Java Utililty Methods Random Number

List of utility methods to do Random Number

Description

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

Method

intgetRandomNumber(int bound)
Produces a random number in the interval (-bound, bound)
return (RANDOM.nextInt(1) * 2 - 1) * RANDOM.nextInt(bound);
intgetRandomNumber(int digit)
Create a random integer number with digit number which was given by the caller.
if (digit <= 0)
    return 0;
int in = 1;
int la = 0;
int sa = 0;
for (int x = 0; x < digit; x++) {
    in = in * 10;
la = in;
sa = la / 10;
return getRandomNumber(sa, la);
StringgetRandomNumber(int len)
get Random Number
StringBuffer sb = new StringBuffer();
int p;
for (int i = 0; i < len; i++) {
    p = getRandomInt(0, m_chars.length);
    if (p < 0)
        p = 0;
    if (p >= m_chars.length)
        p = m_chars.length - 1;
...
StringgetRandomNumber(int len)
return a len not repeat random number String, len must be less than 10
StringBuilder sb = new StringBuilder();
String str = "0123456789";
Random r = new Random();
for (int i = 0; i < len; i++) {
    int num = r.nextInt(str.length());
    sb.append(str.charAt(num));
    str = str.replace((str.charAt(num) + ""), "");
return sb.toString();
StringgetRandomNumber(int length)
get Random Number
String result = "";
Random r = new Random();
while (0 < length) {
    length -= 1;
    result += r.nextInt(9);
return result;
intgetRandomNumber(int length)
get Random Number
Random random = new Random();
return random.nextInt(length);
StringgetRandomNumber(int length)
get Random Number
StringBuffer bu = new StringBuffer();
String[] arr = { "2", "3", "4", "5", "6", "7", "8", "9", "1", "0" };
Random random = new Random();
while (bu.length() < length) {
    String temp = arr[random.nextInt(10)];
    if (bu.indexOf(temp) == -1) {
        bu.append(temp);
return bu.toString();
intgetRandomNumber(int max, boolean include0)
Returns a random number, up to the given maximum.
Random random = new Random();
int i = random.nextInt(max + 1);
return (i == 0 && !include0 ? 1 : i);
SetgetRandomNumber(int max, int count)
get Random Number
Set v = new TreeSet();
Random r = new Random();
boolean b = true;
while (b) {
    v.add(r.nextInt(max));
    if (v.size() == count) {
        b = false;
return v;
intgetRandomNumber(int num)
get Random Number
int randomNumber = generator.nextInt(num);
return randomNumber;