Returns a value between value1 and value2 that can include value1 and value2 - Java java.util

Java examples for java.util:Random

Description

Returns a value between value1 and value2 that can include value1 and value2

Demo Code


//package com.java2s;
import java.util.Random;

public class Main {
    private static final Random RANDOM = new Random();

    /**/*from   www  . j a v a2  s  . c  om*/
     * Returns a value between value1 and value2 that can include value1 and value2
     *
     * @param value1 First value
     * @param value2 Second value
     * @return a value between value1 and value2 that can include value1 and value2
     */
    public static double randomRangeDoubleInclusive(double value1,
            double value2) {
        double min = Math.min(value1, value2);
        double max = Math.max(value1, value2);
        double value = min + RANDOM.nextDouble() * (max - min + 1.0);
        return Math.min(Math.max(value, min), max);
    }
}

Related Tutorials