Package com.ryandw11.structure.utils
Class RandomCollection<E>
java.lang.Object
com.ryandw11.structure.utils.RandomCollection<E>
- Type Parameters:
E
- The type of collection.
A collection where the next value can be obtained at Random.
The way this works is that each item has a weight. Everytime an item is
added to the collection, the weight is added to the total weight value of the collection.
When the next()
method is called is calculates the probability of each item by
the weight/total_weight.
So if one item has a weight of 10 and another of 20, the total weight is 30. The first item has a 33% chance of being selected while the second item has a 66% of spawning. If a third item is added with a weight of 5, these would be the probability table:
Item One | Item Two | Item Three | Total Weight |
---|---|---|---|
10/35 = 28.6% | 20/35 = 57.7% | 5/35 = 16.7% | 35 |
-
Constructor Summary
ConstructorDescriptionConstruct the random collection with a new random.RandomCollection(Random random)
Construct random collection with an existing random. -
Method Summary
-
Constructor Details
-
RandomCollection
public RandomCollection()Construct the random collection with a new random. -
RandomCollection
Construct random collection with an existing random.- Parameters:
random
- The random.
-
-
Method Details
-
add
Add a value to the RandomCollection.View the class description for a detailed explanation of how the probability of this collection works.
- Parameters:
weight
- The weight of the item.result
- The item to add.- Returns:
- The instance of this collection.
-
next
Get the next value randomly based upon defined probabilities.View the class description for a detailed explanation of how the probability of this collection works.
- Returns:
- The next value.
-
isEmpty
public boolean isEmpty()Check if the collection is empty.- Returns:
- If the collection is empty.
-
getMap
Get the internal map.- Returns:
- The internal map.
-
toList
Convert the RandomCollection into a list.- Returns:
- A list of values.
-