public class RouletteSelector extends Object
Simulates selection based on a roulette wheel where all items have a weight expressing the size of their section of the wheel. The size of the wheel is the sum of all item weights. During selection a random number in (0, wheel size) is picked and then the item corresponding to this section of the wheel is selected.
For a set of items with equal weight they will all have the same probability of being selected. Items which are assigned higher weights will have a higher likelihood of being selected (and the opposite holds for lower weights).
The implementation is heavily inspired by original code written by the Apache Software Foundation (http://www.apache.org) for the Hadoop MapReduce project (http://hadoop.apache.org).
Constructor and Description |
---|
RouletteSelector() |
Modifier and Type | Method and Description |
---|---|
static <E> E |
select(List<E> items,
List<Double> weights,
Random random)
Select an item from a given list by roulette selection, where each item has a weight expressing
the size of its section of the roulette wheel.
|
public static <E> E select(List<E> items, List<Double> weights, Random random)
null
is returned.E
- type of itemsitems
- items from which one is to be selectedweights
- item weights (same order as items)random
- random generator (cannot be null
)null
if all weights are zero
or the item list is emptyIllegalArgumentException
- if both lists are not of the same size or if any of the
given weights is < 0NullPointerException
- if items
, weights
or random
are null
or if weights
contains any
null
elementsCopyright © 2016. All rights reserved.