Class RandomSelector<T>
- Type Parameters:
T
- the type of elements being selected over
The random selection is independent between every constructed instance of RandomSelector objects, but for the same instance, multiple calls to getValues() are not independent. Making two calls to consecutive getValues() without an accept() in between will return two new Lists containing the same elements.
A second mode allows for a fixed probability of randomly keeping each item as opposed to a fixed number of samples.
SPECFIELDS:
values : Set : The values chosen based on the Objects observed
observed : int : The number of Objects observed
numElts : int : The number of elements to choose ('k' above)
keepProbability: double : The percentage of elements to keep
selector_mode : {FIXED,PERCENT} : either fixed amount of samples or fixed percent.
Example use:
// randomly selects 100 lines of text from a file
List selectedLines = null;
try {
BufferedReader br = new BufferedReader(new FileReader("myfile.txt"));
RandomSelector selector = new RandomSelector(100);
while (br.ready()) {
selector.accept(br.readLine());
}
selectedLines = selector.getValues();
}
catch (IOException e2) { e2.printStackTrace(); }
-
Constructor Summary
ConstructorDescriptionRandomSelector
(double keepProbability, Random r) Creates a new RandomSelector.RandomSelector
(int numElts) Creates a new RandomSelector.RandomSelector
(int numElts, Random r) Creates a new RandomSelector. -
Method Summary
Modifier and TypeMethodDescriptionvoid
When in fixed sample mode, increments the number of observed elements i by 1, then with probability k / i, the Object 'next' will be added to the currently selected values 'values' where k is equal to 'numElts'.Returns values, modifies none.
-
Constructor Details
-
RandomSelector
public RandomSelector(int numElts) Creates a new RandomSelector.- Parameters:
numElts
- the number of elements intended to be selected from the input elements
-
RandomSelector
Creates a new RandomSelector.- Parameters:
numElts
- the number of elements intended to be selected from the input elementsr
- the seed to give for random number generation
-
RandomSelector
Creates a new RandomSelector.- Parameters:
keepProbability
- the probability that each element is selected from the oncoming Iterationr
- the seed to give for random number generation
-
-
Method Details
-
accept
When in fixed sample mode, increments the number of observed elements i by 1, then with probability k / i, the Object 'next' will be added to the currently selected values 'values' where k is equal to 'numElts'. If the size of values exceeds numElts, then one of the existing elements in values will be removed at random.When in probability mode, adds next to 'values' with probability equal to 'keepProbability'.
- Parameters:
next
- value to be added to this selector
-
getValues
Returns values, modifies none.- Returns:
- values
-