Table of Contents

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

random Random

An instance of Random used to generate the random selection.

collection IList<T>

The collection to choose an item from.

Returns

T

A randomly chosen item from the collection.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown if random or collection is null.

ArgumentOutOfRangeException

Thrown if collection is 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

random Random

An instance of Random used to generate the random selection.

collection T[]

The array to choose an item from.

Returns

T

A randomly chosen item from the collection.

Type Parameters

T

The type of elements in the array.

Exceptions

ArgumentNullException

Thrown if random or collection is null.

ArgumentOutOfRangeException

Thrown if collection is 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

random Random

An instance of Random used to generate random indices for shuffling.

collection IList<T>

The collection to shuffle.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown if random or collection is null.