mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 14:08:48 +00:00
✨ Add ListExtensions.Shuffle<T>()
Shuffle<T>() returns the source enumerable ordered randomly
This commit is contained in:
parent
cd04c1b66f
commit
120ea9562e
@ -50,5 +50,24 @@
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
public static T OneOf<T>(this IList<T> source, Random random) =>
|
||||
random.OneOf(source);
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles an enumerable.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to shuffle.</param>
|
||||
/// <returns>Returns <paramref name="source"/> shuffled.</returns>
|
||||
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) =>
|
||||
source.Shuffle(new Random());
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles an enumerable.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to shuffle.</param>
|
||||
/// <param name="random">The <see cref="Random"/> instance.</param>
|
||||
/// <returns>Returns <paramref name="source"/> shuffled.</returns>
|
||||
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random random) =>
|
||||
source.OrderBy(x => random.Next());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user