Java BigDecimal randomBigDecimalList(BigDecimal min, BigDecimal max, int minLength, int maxLength)

Here you can find the source of randomBigDecimalList(BigDecimal min, BigDecimal max, int minLength, int maxLength)

Description

Returns a list of randomly generated BigDecimals using the minimum and maximum data point values and the minimum and maximum lengths.

License

Apache License

Parameter

Parameter Description
BigDecimal maximum data point value

Return

list with random BigDecimal values

Declaration

public static List<BigDecimal> randomBigDecimalList(BigDecimal min, BigDecimal max, int minLength,
        int maxLength) 

Method Source Code

//package com.java2s;
/**/* ww w. j  a  va2 s. c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.math.BigDecimal;
import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    /**
     * Returns a list of randomly generated BigDecimals using the minimum and maximum
     * data point values and the minimum and maximum lengths.
     * @param    BigDecimal    minimum data point value
     * @param    BigDecimal    maximum data point value
     * @param    int           minimum list length
     * @param    int           maximum list length
     * @return                 list with random BigDecimal values
     */
    public static List<BigDecimal> randomBigDecimalList(BigDecimal min, BigDecimal max, int minLength,
            int maxLength) {

        try {

            if (min.compareTo(max) > 0 || minLength >= maxLength) {
                throw new IndexOutOfBoundsException("Index out of bounds generating random big decimal list.");
            }

            List<BigDecimal> list = new ArrayList<BigDecimal>();

            int length = ThreadLocalRandom.current().nextInt(minLength, maxLength + 1);

            for (int i = 0; i < length; i++) {
                BigDecimal random = min.add(new BigDecimal(Math.random()).multiply(max.subtract(min)));
                list.add(random.setScale(2, BigDecimal.ROUND_HALF_UP));
            }

            return list;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * Returns a list of randomly generated BigDecimals using the minimum and maximum
     * data point values and the minimum and maximum lengths.
     * @param    BigDecimal    minimum data point value
     * @param    BigDecimal    maximum data point value
     * @param    int           minimum list length
     * @param    int           maximum list length
     * @return                 list with random BigDecimal values
     */
    public static List<BigDecimal> randomBigDecimalList(BigDecimal min, BigDecimal max, int length) {

        try {

            if (min.compareTo(max) > 0 || length <= 0) {
                throw new IndexOutOfBoundsException("Index out of bounds generating random big decimal list.");
            }

            List<BigDecimal> list = new ArrayList<BigDecimal>();

            for (int i = 0; i < length; i++) {
                BigDecimal random = min.add(new BigDecimal(Math.random()).multiply(max.subtract(min)));
                list.add(random.setScale(2, BigDecimal.ROUND_HALF_UP));
            }

            return list;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}

Related

  1. percentToFactor(BigDecimal percent)
  2. printBigDecimal(BigDecimal decimal)
  3. printGainHTML(BigDecimal gain)
  4. putBigDecimal(byte[] bytes, int offset, BigDecimal val)
  5. randomBigDecimal()
  6. rangedValue(BigDecimal lower, BigDecimal upper, Random random)
  7. readBigDecimal(byte valueBytes[], int valueLength, int scale)
  8. readBigDecimal(DataInput in)
  9. readBigDecimal(final Object object)