Returns a random double between MIN inclusive and MAX inclusive. - Android java.util

Android examples for java.util:Random Double

Description

Returns a random double between MIN inclusive and MAX inclusive.

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    /**//from w  w w .java 2 s .c  om
     * Returns a random double between MIN inclusive and MAX inclusive.
     * @param min
     *            value inclusive
     * @param max
     *            value inclusive
     * @return an int between 0 inclusive and MAX exclusive.
     */
    public static double getRandomDoubleWithinRange(double min, double max) {
        if (min == max) {
            return max;
        }
        if (max < min) {
            double temp = max;
            max = min;
            min = temp;
        }
        Random r = new Random();
        return min + (max - min) * r.nextDouble();
    }
}

Related Tutorials