java.lang.Object
imagingbook.common.ransac.RandomDraw<T>
- Type Parameters:
T
- element type
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
FieldsModifier and TypeFieldDescriptionstatic final int
Default maximum number of tries before exception is thrown. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor creating its own random generator.RandomDraw
(Random rand) Constructor accepting an existing random generator. -
Method Summary
Modifier and TypeMethodDescriptionT[]
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.
-
Field Details
-
DefaultMaxTries
Default maximum number of tries before exception is thrown.- See Also:
-
-
Constructor Details
-
RandomDraw
Constructor accepting an existing random generator. This may be useful to achieve predictable (repetitive) random behavior.- Parameters:
rand
- a random generator of typeRandom
-
RandomDraw
public RandomDraw()Constructor creating its own random generator.
-
-
Method Details
-
setMaxTries
Set the maximum number of tries.- Parameters:
maxTries
- new maximum number of tries- See Also:
-
drawFrom
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 (seeDefaultMaxTries
). 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 allowedk
- the number of items to draw- Returns:
- an array of k randomly drawn (non-null) items
-