Here you can find the source of getRandomNumberBetween(int min, int max)
Parameter | Description |
---|---|
min | The minimum number that the random number can be. |
max | The maximum number that the random number can be. |
public static int getRandomNumberBetween(int min, int max)
//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; } }