Java Random Double getDoubleInRange(double minValue, double maxValue)

Here you can find the source of getDoubleInRange(double minValue, double maxValue)

Description

It returns a double/Double 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 double/Double value between min and max value (included)

Declaration

public static double getDoubleInRange(double minValue, double 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 ww w. j av  a 2s. c o m*/
     * 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. generateRandomDoubleInRange(double min, double max)
  2. generateRandomLongitude()
  3. getDouble()
  4. getDouble()
  5. getDoubleSpecial()
  6. getRandomDouble()
  7. getRandomDouble(double min, double max)
  8. rand(double seed)