Class RandomDraw<T>

java.lang.Object
imagingbook.common.ransac.RandomDraw<T>
Type Parameters:
T - element type

public class RandomDraw<T> extends Object
An instance of this class randomly draws a set of k unique, non-null elements from a given array of element type T, which may contain null elements (see method drawFrom(Object[], int)). The size of the draw (k) is fixed and must be specified at construction. The resulting sets contain no duplicate elements.
Version:
2022/11/19
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default maximum number of tries before exception is thrown.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor creating its own random generator.
    Constructor accepting an existing random generator.
  • Method Summary

    Modifier and Type
    Method
    Description
    T[]
    drawFrom(T[] items, int k)
    Randomly draws a set of k unique, non-null elements from the specified array of items, ignoring possible null elements.
    void
    setMaxTries(int maxTries)
    Set the maximum number of tries.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • RandomDraw

      public RandomDraw(Random rand)
      Constructor accepting an existing random generator. This may be useful to achieve predictable (repetitive) random behavior.
      Parameters:
      rand - a random generator of type Random
    • RandomDraw

      public RandomDraw()
      Constructor creating its own random generator.
  • Method Details

    • setMaxTries

      public void setMaxTries(int maxTries)
      Set the maximum number of tries.
      Parameters:
      maxTries - new maximum number of tries
      See Also:
    • drawFrom

      public T[] drawFrom(T[] items, int k)
      Randomly draws a set of k unique, non-null elements from the specified array of items, ignoring possible null elements. An exception is thrown if the maximum number of tries is exceeded (see DefaultMaxTries). The returned array contains no null elements and no duplicates. Example:
       Integer[] numbers = { null, 1, 2, null, 3, 4, 5, 6, 7, null, null, null, 8, 9, 10, null };
       RandomDraw<Integer> rd = new RandomDraw<>();
       Integer[] draw = rd.drawFrom(numbers, 2);
       
      Parameters:
      items - an array of elements of type T, null elements being allowed
      k - the number of items to draw
      Returns:
      an array of k randomly drawn (non-null) items