get Smaller Random Double - Java java.util

Java examples for java.util:Random

Description

get Smaller Random Double

Demo Code

/*/*from  w w  w .j a  v a 2s.  c  o  m*/
 * *
 *    Copyright 2011-2014 Fr?d?ric Bapst & HEIA-FR students
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 */
//package com.java2s;
import java.util.Random;
import static java.lang.Double.*;

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

    /**
     * @param value reference fot the computation
     *              PRE : value is part of [0.0,Double.MAX_VALUE[
     *
     * @return a double smaller than value and positive
     *
     * For example with a value 20.0 the return could be 10.0
     */
    public static double getSmallerRndDouble(double value) {
        assert (value >= 0.0);
        assert (value < MAX_VALUE);
        if (value >= 0.0 && value < MAX_VALUE) {
            return r.nextDouble() * value;
        }
        return NaN;
    }
}

Related Tutorials