Java List Random Item makeRandomList(int arraySize, boolean unique)

Here you can find the source of makeRandomList(int arraySize, boolean unique)

Description

make Random List

License

Open Source License

Declaration

public static ArrayList<Integer> makeRandomList(int arraySize, boolean unique) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.HashSet;

import java.util.Random;

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

    public static ArrayList<Integer> makeRandomList(int arraySize, boolean unique) {
        int[] values = makeRandomArray(arraySize, unique);
        ArrayList<Integer> result = new ArrayList<Integer>();
        for (int i = 0; i < values.length; i++) {
            result.add(values[i]);//from  ww  w .  j a v a 2s  . com
        }

        return result;
    }

    public static int[] makeRandomArray(int arraySize, boolean unique) {
        int[] values = new int[arraySize];
        HashSet<Integer> used = new HashSet<Integer>();

        for (int i = 0; i < arraySize; i++) {
            int toInsert = r.nextInt(10000);
            while (unique && used.contains(toInsert)) {
                toInsert = r.nextInt(10000);
            }
            values[i] = toInsert;
            used.add(toInsert);
        }
        return values;
    }
}

Related

  1. getRandomFromList(List list)
  2. getRandomIdxList(int minIdx, int maxIdx)
  3. getRandomIntBetween(int min, int max, List excludeList)
  4. listChooseOne(List list)
  5. makeListOfIntegersInRange(Integer size, Integer maxOfRange)
  6. newRandom(int size, int price, List oldRandomList)
  7. newRandomIntegerList(int size)
  8. newRandomStringList(int size, String... strings)
  9. newRandomV1(int size, int price, List oldRandomList)