Java Random Double random(double low, double high)

Here you can find the source of random(double low, double high)

Description

Create a random number between two double bounds.

License

Open Source License

Parameter

Parameter Description
low the low bound
high the high bound

Return

the random number

Declaration

public static double random(double low, double high) 

Method Source Code

//package com.java2s;
/**/* w w w .  j a v a  2 s.c o  m*/
 * This file is part of PaxmlCore.
 *
 * PaxmlCore is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PaxmlCore is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with PaxmlCore.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Create a random number between two double bounds.
     * 
     * @param low
     *            the low bound
     * @param high
     *            the high bound
     * @return the random number
     */
    public static double random(double low, double high) {
        if (low == high) {
            return low;
        } else if (low > high) {
            double tmp = low;
            low = high;
            high = tmp;
        }
        double range = high - low;
        return low + Math.random() * range;
    }

    /**
     * Create a random number between two long bounds.
     * 
     * @param low
     *            the low bound
     * @param high
     *            the high bound
     * @return the random number
     */
    public static long random(long low, long high) {
        return Math.round(random(low + 0.0, high));
    }
}

Related

  1. randDouble(double min, double max)
  2. randDouble(double min, double max)
  3. randDouble(int m, int n)
  4. randIn(double min, double max)
  5. RandInRange(double start, double end)
  6. random(double min, double max)
  7. random(double min, double max)
  8. random(final double min, final double max)
  9. randomBetween(double arg1, double arg2)