Class RandomListExtensions
- Namespace
- Stride.CommunityToolkit.Mathematics
- Assembly
- Stride.CommunityToolkit.dll
Provides extension methods for the Random class to facilitate random selection and shuffling of collections.
public static class RandomListExtensions
- Inheritance
-
RandomListExtensions
Remarks
These extensions add functionality for randomly selecting an element from a collection or array, and for shuffling elements in place. Useful for games, simulations, or any scenario requiring random operations on lists or arrays.
Methods
Choose<T>(Random, IList<T>)
Selects a random item from a given collection.
public static T Choose<T>(this Random random, IList<T> collection)
Parameters
randomRandomAn instance of Random used to generate the random selection.
collectionIList<T>The collection to choose an item from.
Returns
- T
A randomly chosen item from the
collection.
Type Parameters
TThe type of elements in the collection.
Exceptions
- ArgumentNullException
Thrown if
randomorcollectionis null.- ArgumentOutOfRangeException
Thrown if
collectionis empty.
Choose<T>(Random, params T[])
Selects a random item from a given array.
public static T Choose<T>(this Random random, params T[] collection)
Parameters
randomRandomAn instance of Random used to generate the random selection.
collectionT[]The array to choose an item from.
Returns
- T
A randomly chosen item from the
collection.
Type Parameters
TThe type of elements in the array.
Exceptions
- ArgumentNullException
Thrown if
randomorcollectionis null.- ArgumentOutOfRangeException
Thrown if
collectionis empty.
Shuffle<T>(Random, IList<T>)
Shuffles the elements of the specified collection in place using the Fisher-Yates shuffle algorithm.
public static void Shuffle<T>(this Random random, IList<T> collection)
Parameters
randomRandomAn instance of Random used to generate random indices for shuffling.
collectionIList<T>The collection to shuffle.
Type Parameters
TThe type of elements in the collection.
Exceptions
- ArgumentNullException
Thrown if
randomorcollectionis null.