Class RandomWithoutDuplicates
java.lang.Object
edu.gvsu.kurmasz.warszawa.listgen.IntegerListGenerator
edu.gvsu.kurmasz.warszawa.listgen.RandomWithDuplicates
edu.gvsu.kurmasz.warszawa.listgen.RandomWithoutDuplicates
public class RandomWithoutDuplicates extends RandomWithDuplicates
Generate lists of unique random integers within a specified range.
- Author:
- Zachary Kurmas
-
Constructor Summary
Constructors Constructor Description RandomWithoutDuplicates()Default constructor.RandomWithoutDuplicates(java.util.Random rin)Constructor allowing user to specify whichjava.util.Randomobject to use. -
Method Summary
Modifier and Type Method Description int[]generateIntArray(int min, int max, int amount)Generate an array of unique random integers containing the values betweenstartandstop(inclusive).Methods inherited from class edu.gvsu.kurmasz.warszawa.listgen.RandomWithDuplicates
generateLongArrayMethods inherited from class edu.gvsu.kurmasz.warszawa.listgen.IntegerListGenerator
generateIntList, generateLongList
-
Constructor Details
-
RandomWithoutDuplicates
public RandomWithoutDuplicates(java.util.Random rin)Constructor allowing user to specify whichjava.util.Randomobject to use.- Parameters:
rin- the desiredjava.util.Randomobject.
-
RandomWithoutDuplicates
public RandomWithoutDuplicates()Default constructor. Generates a newRandomobject.
-
-
Method Details
-
generateIntArray
public int[] generateIntArray(int min, int max, int amount)Generate an array of unique random integers containing the values betweenstartandstop(inclusive). At present, we guarantee uniqueness in one of two ways:- If
amountis a large percentage of the range (i.e.,max - min), then we simply permute all the values in the range then choose the firstamountvalues. This technique works well, because we don't waste a lot of memory or effort (almost all of the permuted values will be used). - If
amountis a small percentage of the range (i.e.,max - min), then we just draw values until we get the desired number of unique values. This technique works well because, whenamountis a small percentage of the range, the probability of drawing a "used" number is small.
- Overrides:
generateIntArrayin classRandomWithDuplicates- Parameters:
min- the minimum value that may appearmax- the maximum value that may appear. Must be>= minand<= (min + Integer.MAX_VALUE)amount- the number of integers to generate. Must be>= 1.- Returns:
- an array of long integers.
- Throws:
java.lang.IllegalArgumentException- if the parameters specify an array that is too large.
- If
-