RogueSharp
Show / Hide Table of Contents

Class WeightedPool<T>

A weighted collection from which items of type T can be chosen at random. Although the item is picked at random, weights will be considered. Items with higher weights will be more likely to be picked than items with lower weights.

Inheritance
System.Object
WeightedPool<T>
Implements
IWeightedPool<T>
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: RogueSharp.Random
Assembly: RogueSharp.dll
Syntax
public class WeightedPool<T> : IWeightedPool<T>
Type Parameters
Name Description
T

The type of item to be stored in the WeightedPool

Examples

A treasure lookup table in a board game could be implemented as a WeightedPool. A booster pack for a collectable card game could be implemented as a WeightedPool.

Constructors

| Improve this Doc View Source

WeightedPool()

Construct a new weighted pool using the default random number generator provided with .NET This constructor does not take a clone function so Choose() cannot be called, only Draw()

Declaration
public WeightedPool()
| Improve this Doc View Source

WeightedPool(IRandom, Func<T, T>)

Construct a new weighted pool using the provided random number generator and clone function

Declaration
public WeightedPool(IRandom random, Func<T, T> cloneFunc = null)
Parameters
Type Name Description
IRandom random

A class implementing IRandom that will be used to generate pseudo-random numbers necessary to pick items from the WeightedPool

System.Func<T, T> cloneFunc

A function that takes an object of type T and returns a clone of the item. When comparing the original object and the clone, all properties should be equal. The clone will have a different reference than the original object.

Exceptions
Type Condition
System.ArgumentNullException

Thrown when provided "random" argument is null

Properties

| Improve this Doc View Source

Count

How many items are in the WeightedPool

Declaration
public int Count { get; }
Property Value
Type Description
System.Int32

Methods

| Improve this Doc View Source

Add(T, Int32)

Add an item of type T to the WeightedPool with the given weight

Declaration
public void Add(T item, int weight)
Parameters
Type Name Description
T item

The item to add to the WeightedPool

System.Int32 weight

The chance that the item will be picked at random from the pool when weighted against all other items. Higher weights mean it is more likely to be picked.

Exceptions
Type Condition
System.ArgumentNullException

Thrown when provided "item" argument is null

System.ArgumentException

Thrown when provided "weight" argument is not greater than 0

System.OverflowException

Thrown when adding the weight of the new item to the pool exceeds Int32.MaxValue

| Improve this Doc View Source

Choose()

Choose an item at random from the pool, keeping weights into consideration. The item itself will remain in the pool and a clone of the item selected will be returned.

Declaration
public T Choose()
Returns
Type Description
T

A clone of the item of type T from the WeightedPool

Exceptions
Type Condition
System.InvalidOperationException

Thrown when a clone function was not defined when the pool was constructed

System.InvalidOperationException

Thrown when the pool is empty

System.InvalidOperationException

Thrown when the random lookup is greater than the total weight. Could only happen if a bad implementation of IRandom were provided

| Improve this Doc View Source

Clear()

Remove all items from the WeightedPool. The WeightedPool will be empty after calling this method.

Declaration
public void Clear()
| Improve this Doc View Source

Draw()

Take an item at random from the pool, keeping weights into consideration. The item will be removed from the pool.

Declaration
public T Draw()
Returns
Type Description
T

An item of type T from the WeightedPool

Exceptions
Type Condition
System.InvalidOperationException

Thrown when the pool is empty

System.InvalidOperationException

Thrown when the random lookup is greater than the total weight. Could only happen if a bad implementation of IRandom were provided

Implements

IWeightedPool<T>
  • Improve this Doc
  • View Source
RogueSharp © 2014-2020 Faron Bracy
Back to top