Here you can find the source of randomIntegerList(int min, int max, int minLength, int maxLength)
Parameter | Description |
---|
public static List<Integer> randomIntegerList(int min, int max, int minLength, int maxLength)
//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; } } }