Java ThreadLocalRandom 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

Apache 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: Apache License 

import java.util.Random;

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    /**/*  w ww  .j a  v a2s.  c o  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) {

        // NOTE: This will (intentionally) not run as written so that folks
        // copy-pasting have to think about how to initialize their
        // Random instance.  Initialization of the Random instance is outside
        // the main scope of the question, but some decent options are to have
        // a field that is initialized once and then re-used as needed or to
        // use ThreadLocalRandom (if using at least Java 1.7).
        Random rand = ThreadLocalRandom.current();

        // 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. getThreadLocalRandom()
  2. isRandomOccurrence(int pcLikelihood)
  3. moveInRadius(double[] position, double radius)
  4. parseSeed(String seedString)
  5. rand(final Collection items)
  6. random()
  7. random()
  8. random()
  9. random(char lower, char upper, int length)