Java ThreadLocalRandom randomIntegerList(int min, int max, int minLength, int maxLength)

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

Description

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

License

Apache License

Parameter

Parameter Description

Return

list with random integer values

Declaration

public static List<Integer> randomIntegerList(int min, int max, int minLength, int maxLength) 

Method Source Code

//package com.java2s;
/**//from  w ww.j  av a 2s . 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.util.ArrayList;

import java.util.List;

import java.util.concurrent.ThreadLocalRandom;

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

        try {

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

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

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

            for (int i = 0; i < length; i++) {
                list.add(ThreadLocalRandom.current().nextInt(min, max + 1));
            }

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

    }

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

        try {

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

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

            for (int i = 0; i < length; i++) {
                list.add(ThreadLocalRandom.current().nextInt(min, max + 1));
            }

            return list;

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

    }
}

Related

  1. randomIndexs(int count, int bound)
  2. randomInRange(int min, int max)
  3. randomInt()
  4. randomInt(int bound)
  5. randomInteger(int min, int max)
  6. randomLongs(final long count, final long min, final long max)
  7. randomLongsNonDuplicates(final long count, final long min, final long max)
  8. randomNum(int min, int max)
  9. randomNums(int min, int max, int count)