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.Random
object 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 betweenstart
andstop
(inclusive).Methods inherited from class edu.gvsu.kurmasz.warszawa.listgen.RandomWithDuplicates
generateLongArray
Methods 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.Random
object to use.- Parameters:
rin
- the desiredjava.util.Random
object.
-
RandomWithoutDuplicates
public RandomWithoutDuplicates()Default constructor. Generates a newRandom
object.
-
-
Method Details
-
generateIntArray
public int[] generateIntArray(int min, int max, int amount)Generate an array of unique random integers containing the values betweenstart
andstop
(inclusive). At present, we guarantee uniqueness in one of two ways:- If
amount
is a large percentage of the range (i.e.,max - min
), then we simply permute all the values in the range then choose the firstamount
values. 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
amount
is 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, whenamount
is a small percentage of the range, the probability of drawing a "used" number is small.
- Overrides:
generateIntArray
in classRandomWithDuplicates
- Parameters:
min
- the minimum value that may appearmax
- the maximum value that may appear. Must be>= min
and<= (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
-