Java BigDecimal rangedValue(BigDecimal lower, BigDecimal upper, Random random)

Here you can find the source of rangedValue(BigDecimal lower, BigDecimal upper, Random random)

Description

Generates a new BigDecimal that lies within the specified range.

License

Open Source License

Parameter

Parameter Description
lower The inclusive lower limit.
upper The exclusive upper limit.
random The Random used to generate the fraction.

Return

A new BigDecimal that lies within the specified range.

Declaration

static BigDecimal rangedValue(BigDecimal lower, BigDecimal upper,
        Random random) 

Method Source Code

//package com.java2s;
/*//w ww  .j  a  va 2  s.c  o  m
 * MIT License
 *
 * Copyright (c) 2016-2017 Subhomoy Haldar (github.com/Subh0m0y)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.math.BigDecimal;

import java.util.Random;

public class Main {
    /**
     * Generates a new BigDecimal that lies within the specified range.
     *
     * @param lower  The inclusive lower limit.
     * @param upper  The exclusive upper limit.
     * @param random The Random used to generate the fraction.
     * @return A new BigDecimal that lies within the specified range.
     */
    static BigDecimal rangedValue(BigDecimal lower, BigDecimal upper,
            Random random) {
        BigDecimal difference = upper.subtract(lower);
        BigDecimal fraction = BigDecimal.valueOf(random.nextDouble());
        return lower.add(difference.multiply(fraction));
    }
}

Related

  1. printBigDecimal(BigDecimal decimal)
  2. printGainHTML(BigDecimal gain)
  3. putBigDecimal(byte[] bytes, int offset, BigDecimal val)
  4. randomBigDecimal()
  5. randomBigDecimalList(BigDecimal min, BigDecimal max, int minLength, int maxLength)
  6. readBigDecimal(byte valueBytes[], int valueLength, int scale)
  7. readBigDecimal(DataInput in)
  8. readBigDecimal(final Object object)
  9. releaseNull(BigDecimal amount)