Java Random Int generateRandomInteger(int aStart, int aEnd, Random aRandom)

Here you can find the source of generateRandomInteger(int aStart, int aEnd, Random aRandom)

Description

Erzeugt eine Zufallszahl

License

Open Source License

Parameter

Parameter Description
aStart a parameter
aEnd a parameter
aRandom a parameter

Declaration

public static int generateRandomInteger(int aStart, int aEnd, Random aRandom) 

Method Source Code

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

import java.util.Random;

public class Main {
    /**//  w w  w  . j  a  v a2 s.com
     * Erzeugt eine Zufallszahl
     * 
     * @param aStart
     * @param aEnd
     * @param aRandom
     * @return
     */
    public static int generateRandomInteger(int aStart, int aEnd, Random aRandom) {
        if (aStart > aEnd) {
            throw new IllegalArgumentException("Start cannot exceed End.");
        }
        // get the range, casting to long to avoid overflow problems
        long range = (long) aEnd - (long) aStart + 1;
        // compute a fraction of the range, 0 <= frac < range
        long fraction = (long) (range * aRandom.nextDouble());
        int randomNumber = (int) (fraction + aStart);

        return randomNumber;
    }
}

Related

  1. generateRandomInt(final int max)
  2. generateRandomInt(int min, int max)
  3. generateRandomInteger(int start, int end)
  4. generateRandomIntegerArray(int size, int min, int max)
  5. generateRandomIntegers(int numbersNeeded, int bound)
  6. generateRandomIntInRange(int min, int max)