diff --git a/src/BitzArt.LinqExtensions/Extensions/ShuffleExtensions.cs b/src/BitzArt.LinqExtensions/Extensions/ShuffleExtensions.cs index c46f1b9..3741aa4 100644 --- a/src/BitzArt.LinqExtensions/Extensions/ShuffleExtensions.cs +++ b/src/BitzArt.LinqExtensions/Extensions/ShuffleExtensions.cs @@ -15,16 +15,33 @@ public static class ShuffleExtensions /// Shuffles the elements of the collection /// using /// + /// The collection to shuffle. + /// The seed to use for the random number generator. /// A new IQueryable with the elements shuffled. - public static IQueryable Shuffle(this IQueryable source) - => source.Shuffle().AsQueryable(); + public static IQueryable Shuffle(this IQueryable source, int? seed = null) + => source.Shuffle(seed).AsQueryable(); /// /// Shuffles the elements of the collection /// using /// + /// The collection to shuffle. + /// The seed to use for the random number generator. /// A new collection with the elements shuffled. - public static IEnumerable Shuffle(this IEnumerable source) + public static IEnumerable Shuffle(this IEnumerable source, int? seed = null) + { + var random = seed.HasValue ? new Random(seed.Value) : _random; + return source.Shuffle(random); + } + + /// + /// Shuffles the elements of the collection + /// using + /// + /// The collection to shuffle. + /// The random number generator to use. + /// A new collection with the elements shuffled. + public static IEnumerable Shuffle(this IEnumerable source, Random random) { // preserve the original collection var list = new List(source); @@ -33,7 +50,7 @@ public static IEnumerable Shuffle(this IEnumerable source) while (n > 1) { n--; - var k = _random.Next(n + 1); + var k = random.Next(n + 1); (list[n], list[k]) = (list[k], list[n]); } return list;