From 120ea9562ef50a94572b606b56d44e1aacd745a3 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 16 Nov 2019 02:12:15 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ListExtensions.Shuffle()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Shuffle() returns the source enumerable ordered randomly --- X10D/ListExtensions.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/X10D/ListExtensions.cs b/X10D/ListExtensions.cs index 586e4af..c67df16 100644 --- a/X10D/ListExtensions.cs +++ b/X10D/ListExtensions.cs @@ -50,5 +50,24 @@ /// Returns a random element of type from . public static T OneOf(this IList source, Random random) => random.OneOf(source); + + /// + /// Shuffles an enumerable. + /// + /// The collection type. + /// The collection to shuffle. + /// Returns shuffled. + public static IEnumerable Shuffle(this IEnumerable source) => + source.Shuffle(new Random()); + + /// + /// Shuffles an enumerable. + /// + /// The collection type. + /// The collection to shuffle. + /// The instance. + /// Returns shuffled. + public static IEnumerable Shuffle(this IEnumerable source, Random random) => + source.OrderBy(x => random.Next()); } }