Java Random Int randomIntInRange(int lowerBound, int upperBound)

Here you can find the source of randomIntInRange(int lowerBound, int upperBound)

Description

random Int In Range

License

Open Source License

Declaration

public static int randomIntInRange(int lowerBound, int upperBound) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int randomIntInRange(int lowerBound, int upperBound) {
        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;//from   ww  w  .ja  va 2 s  .  co  m
        float r = (float) rand;
        return Math.round(r);
    }
}

Related

  1. randomInteger(int low, int high)
  2. randomInteger(int min, int max)
  3. randomIntFromInterval(int min, int max)
  4. randomIntFromRange(int min, int max)
  5. randomIntInInterval(int lower, int upper)
  6. randomIntRange(int low, int high)
  7. randomIntWithDigits(int digits)
  8. randomKey(int sLen)
  9. randomMatrix(int x, int y)