T
- the type of elements being selected over@Deprecated
public class RandomSelector<T>
extends java.lang.Object
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:
current_values : Set : The values chosen based on the Objects observed
number_observed : int : The number of Objects observed
number_to_take : int : The number of elements to choose ('k' above)
keep_probability: 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 and Description |
---|
RandomSelector(double keep_probability,
java.util.Random r)
Deprecated.
|
RandomSelector(int num_elts)
Deprecated.
|
RandomSelector(int num_elts,
java.util.Random r)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
accept(T next)
Deprecated.
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
'current_values' where k is equal to 'number_to_take'.
|
java.util.List<T> |
getValues()
Deprecated.
Returns current_values, modifies none.
|
public RandomSelector(int num_elts)
num_elts
- the number of elements intended to be selected from the input elements
Sets 'number_to_take' = num_elts
public RandomSelector(int num_elts, java.util.Random r)
num_elts
- the number of elements intended to be selected from the input elementsr
- the seed to give for random number generation.
Sets 'number_to_take' = num_elts.
public RandomSelector(double keep_probability, java.util.Random r)
keep_probability
- the probability that each element is selected from the oncoming
Iterationr
- the seed to give for random number generationpublic void accept(T next)
When in probability mode, adds next to 'current_values' with probability equal to 'keep_probability'.
next
- value to be added to this selectorpublic java.util.List<T> getValues()