Java Random Int randInt(int min, int max)

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

Description

Returns a pseudo-random number between min and max, inclusive.

License

Open Source License

Parameter

Parameter Description
min Minimum value
max Maximum value. Must be greater than min.

Return

Integer between min and max, inclusive.

Declaration

public static int randInt(int min, int max) 

Method Source Code


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

import java.util.Random;

public class Main {
    private static Random rand = new Random();

    /**/*from ww  w. j ava 2  s.co m*/
     * Returns a pseudo-random number between min and max, inclusive.
     * The difference between min and max can be at most
     * <code>Integer.MAX_VALUE - 1</code>.
     *
     * @param min Minimum value
     * @param max Maximum value.  Must be greater than min.
     * @return Integer between min and max, inclusive.
     * @see java.util.Random#nextInt(int)
     */
    public static int randInt(int min, int max) {

        // Usually this can be a field rather than a method variable
        //Random rand = new Random();
        rand.setSeed(System.currentTimeMillis());

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }
}

Related

  1. randInt(int min, int max)
  2. randInt(int min, int max)
  3. randInt(int min, int max)
  4. randInt(int min, int max)
  5. randInt(int min, int max)
  6. randInt(int min, int max)
  7. randInt(int n)
  8. randInt(int n)
  9. randInt(int n)