Java Random Number getRandomNumberBetween(int min, int max)

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

Description

Returns a random int between min and max, inclusive.

License

Open Source License

Parameter

Parameter Description
min The minimum number that the random number can be.
max The maximum number that the random number can be.

Return

A random int between min and max, inclusive.

Declaration

public static int getRandomNumberBetween(int min, int max) 

Method Source Code

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

import java.util.Random;

public class Main {
    /** The random number stream used by this helper. */
    public final static Random rand = new Random();

    /**/*from ww w .j av  a 2 s .  com*/
     * Returns a random int between min and max, inclusive.
     * @param min The minimum number that the random number can be.
     * @param max The maximum number that the random number can be.
     * @return A random int between min and max, inclusive.
     */
    public static int getRandomNumberBetween(int min, int max) {
        return useRandomForNumberBetween(rand, min, max);
    }

    /**
     * Returns a random float between min and max, inclusive.
     * @param min The minimum number that the random number can be.
     * @param max The maximum number that the random number can be.
     * @return A random float between min and max, inclusive.
     */
    public static float getRandomNumberBetween(float min, float max) {
        return useRandomForNumberBetween(rand, min, max);
    }

    /**
     * Using a specified random stream, returns a random int between min and max, inclusive.
     * @param random The random stream of numbers to get a random number from.
     * @param min The minimum number that the random number can be.
     * @param max The maximum number that the random number can be.
     * @return A random int between min and max, inclusive.
     */
    public static int useRandomForNumberBetween(Random random, int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * Using a specified random stream, returns a random float between min and max, inclusive.
     * @param random The random stream of numbers to get a random number from.
     * @param min The minimum number that the random number can be.
     * @param max The maximum number that the random number can be.
     * @return A random float between min and max, inclusive.
     */
    public static float useRandomForNumberBetween(Random random, float min, float max) {
        return random.nextFloat() * (max - min) + min;
    }
}

Related

  1. getRandomNumber(int max, boolean include0)
  2. getRandomNumber(int max, int count)
  3. getRandomNumber(int num)
  4. getRandomNumber(int randomCount)
  5. getRandomNumber(int startNumber, int endNumber)
  6. getRandomNumberFixLength()
  7. getRandomNumberGenerator()
  8. getRandomNumberInRange(Random random, int min, int max)
  9. getRandomNumbers(int range, int count, Random rnd)