Method will generate a set of integers between the minimum and maximum of the requested size. - Java java.lang

Java examples for java.lang:int Array

Description

Method will generate a set of integers between the minimum and maximum of the requested size.

Demo Code


//package com.java2s;
import java.util.ArrayList;

public class Main {


    /**/*from w w w.  ja v  a 2s.c  o  m*/
     * Method will generate a set of integers between the minimum and maximum 
     * of the requested size.
     * 
     * @param size - the number of elements for the array
     * @param minimum - the lower value of the range of integers to generate
     * @param maximum - the upper value of the range of integers to generate
     * @param uniqueElements - flag for unique values 
     * @return 
     */
    public static ArrayList<Integer> createSet(int size, int minimum,
            int maximum, boolean uniqueElements) {
        boolean isDone = false;
        int i = 0;
        ArrayList<Integer> ReturnList = null;
        if (size > 0) {
            ReturnList = new ArrayList<Integer>();
            boolean isUnique = false;
            while (!isDone) {
                int randomValue = (int) (Math.random() * (maximum - minimum))
                        + minimum;
                isUnique = true;
                for (int j = 0; j < i && isUnique; j++) {
                    isUnique = randomValue != ReturnList.get(j);
                }

                if (isUnique || !uniqueElements) {
                    ReturnList.add(randomValue);
                    i++;
                }

                isDone = (i == size);
            }
        }
        return ReturnList;
    }
}

Related Tutorials