Here you can find the source of getLongInRange(long minValue, long maxValue)
Parameter | Description |
---|---|
minValue | The minimum value for the returned value |
maxValue | The maximum value for the returned value |
public static long getLongInRange(long minValue, long maxValue)
//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; } }