Java Random Int generateRandomInteger(int start, int end)

Here you can find the source of generateRandomInteger(int start, int end)

Description

Generates a random integer in the interval [start, end] (interval includes both start and end) and returns it.

License

Open Source License

Parameter

Parameter Description
start The smallest possible number that can be generated.
end The largest possible number that can be generated.

Return

A randomly generated integer.

Declaration

public static int generateRandomInteger(int start, int end) 

Method Source Code

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

import java.util.Random;

public class Main {
    private static Random _randomNumberGenerator;

    /**/*from  w w  w.jav  a  2 s. c om*/
     * Generates a random integer in the interval [start, end] (interval includes both start and end)
     * and returns it.
     * @param start The smallest possible number that can be generated.
     * @param end The largest possible number that can be generated.
     * @return A randomly generated integer.
     */
    public static int generateRandomInteger(int start, int end) {
        int range = end - start;
        if (_randomNumberGenerator == null)
            _randomNumberGenerator = new Random();
        if (range > 0)
            return _randomNumberGenerator.nextInt(range) + start;
        return start;
    }
}

Related

  1. generateRandomInt(final int max)
  2. generateRandomInt(int min, int max)
  3. generateRandomInteger(int aStart, int aEnd, Random aRandom)
  4. generateRandomIntegerArray(int size, int min, int max)
  5. generateRandomIntegers(int numbersNeeded, int bound)
  6. generateRandomIntInRange(int min, int max)
  7. generateRandomIntObj(int range, int noOfElements)