Click or drag to resize
EnumerableExtensionMethodsRandomWeightedItemT Method
Returns a random weighted item from the specified sequence. Uses the specified function to get the weight of an item. Idea was taken from http://stackoverflow.com/questions/17912005/quick-way-of-selecting-a-random-item-from-a-list-with-varying-probabilities-ba

Namespace: Slash.Collections.Extensions
Assembly: Slash.Collections (in Slash.Collections.dll) Version: 1.0.6018.40177
Syntax
public static T RandomWeightedItem<T>(
	this IEnumerable<T> sequence,
	Func<T, float> getWeight,
	Random random
)

Parameters

sequence
Type: System.Collections.GenericIEnumerableT
Sequence to get item from.
getWeight
Type: SystemFuncT, Single
Function to get weight of an item.
random
Type: SystemRandom
Random number generator.

Type Parameters

T
Type of sequence items.

Return Value

Type: T
Time-dependent random item.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableT. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
See Also