apycazo
5/19/2013 - 1:25 PM

Random number generator with interval.

Random number generator with interval.

public class Rand {

  public static void main(String[] args) {
    System.out.println(Rand.randInRangeInc(Integer.valueOf(args[0]),
      Integer.valueOf(args[1])));
    System.out.println(Rand.randInRangeInc(Double.valueOf(args[0]),
      Double.valueOf(args[1])));
  }

  /**
  * @param min The (included) lower bound of the range
  * @param max The (included) upper bound of the range
  *
  * @return The random value in the range
  */
  public static int randInRangeInc(int min, int max) {
    return min + (int) (Math.random() * (max - min));
  }

  /**
  * @param min The (included) lower bound of the range
  * @param max The (included) upper bound of the range
  *
  * @return The random value in the range
  */
  public static double randInRangeInc(double min, double max) {
    return min + (Math.random() * (max - min));
  }
}