Class RandomWithoutDuplicates

java.lang.Object

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 which java.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 between start and stop (inclusive).

    Methods inherited from class edu.gvsu.kurmasz.warszawa.listgen.RandomWithDuplicates

    generateLongArray

    Methods inherited from class edu.gvsu.kurmasz.warszawa.listgen.IntegerListGenerator

    generateIntList, generateLongList

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RandomWithoutDuplicates

      public RandomWithoutDuplicates​(java.util.Random rin)
      Constructor allowing user to specify which java.util.Random object to use.
      Parameters:
      rin - the desired java.util.Random object.
    • RandomWithoutDuplicates

      public RandomWithoutDuplicates()
      Default constructor. Generates a new Random object.
  • Method Details

    • generateIntArray

      public int[] generateIntArray​(int min, int max, int amount)
      Generate an array of unique random integers containing the values between start and stop (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 first amount 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, when amount is a small percentage of the range, the probability of drawing a "used" number is small.
      Overrides:
      generateIntArray in class RandomWithDuplicates
      Parameters:
      min - the minimum value that may appear
      max - 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.