Java Random Long getLongInRange(long minValue, long maxValue)

Here you can find the source of getLongInRange(long minValue, long maxValue)

Description

It returns a long/Long value between min and max value (included).

License

Open Source License

Parameter

Parameter Description
minValue The minimum value for the returned value
maxValue The maximum value for the returned value

Return

A long/Long value between min and max value (included).

Declaration

public static long getLongInRange(long minValue, long maxValue) 

Method Source Code

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

import java.util.Random;

public class Main {
    /** Random number generator */
    private static final Random RANDOM = new Random();

    /**/*from  w w w  .java 2 s .c  o m*/
     * It returns a long/Long value between min and max value (included).
     * 
     * @param minValue
     *            The minimum value for the returned value
     * @param maxValue
     *            The maximum value for the returned value
     * @return A long/Long value between min and max value (included).
     */
    public static long getLongInRange(long minValue, long maxValue) {
        return (long) (getDoubleInRange(minValue - 0.5, maxValue + 0.5
                - (1 / Long.MAX_VALUE)) + 0.5);
    }

    /**
     * It returns a double/Double value between min and max value (included).
     * 
     * @param minValue
     *            The minimum value for the returned value
     * @param maxValue
     *            The maximum value for the returned value
     * @return A double/Double value between min and max value (included)
     */
    public static double getDoubleInRange(double minValue, double maxValue) {

        // This can happen. It's a way to specify a precise value
        if (minValue == maxValue) {
            return minValue;
        }
        double retValue;
        double margin = (maxValue - minValue + 0.1);
        do {
            retValue = minValue + RANDOM.nextDouble() * margin;
        } while (retValue > maxValue);
        return retValue;
    }
}

Related

  1. generateRandomBySampleSeed(long sampleSeed, long fallbackValue)
  2. generateRandomLongValue()
  3. getLong()
  4. getLong()
  5. getLongBetween(long n, long m)
  6. getLongSpecial()
  7. getLongSpecialCases()
  8. getRandomLong()
  9. randLong()