Java Random Int randBetween(int min, int max)

Here you can find the source of randBetween(int min, int max)

Description

rand Between

License

Apache License

Parameter

Parameter Description
min the mininum returned value
max the maxinum returned value

Return

a random integer between min and max (both included)

Declaration

public static int randBetween(int min, int max) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*  w  ww.  j a  v  a  2s  .c om*/
     * @param min the mininum returned value
     * @param max the maxinum returned value
     * @return a random integer between <code>min</code> and <code>max</code> (both included)
     */
    public static int randBetween(int min, int max) {
        if (max < min)
            throw new IllegalArgumentException("'max' < 'min' is not legal");
        return (int) (Math.random() * (max - min) + min);
    }

    public static long randBetween(long min, long max) {
        if (max < min)
            throw new IllegalArgumentException("'max' < 'min' is not legal");
        return (long) (Math.random() * (max - min) + min);
    }

    public static float randBetween(float min, float max) {
        if (max < min)
            throw new IllegalArgumentException("'max' < 'min' is not legal");
        return (float) (Math.random() * (max - min) + min);
    }
}

Related

  1. getRandomIntFromRange(int min, int max)
  2. getRandomInts(int number)
  3. getRandomIntStr(int len)
  4. rand(int min, int max)
  5. rand(int size)
  6. randInRangeInc(int min, int max)
  7. randInt()
  8. randInt()
  9. randInt(int l)