diff --git a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs new file mode 100644 index 00000000..52f22889 --- /dev/null +++ b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs @@ -0,0 +1,32 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; + +public static partial class Extensions +{ + private static readonly Random _random = new Random(); + + /// + /// An IList<T> extension method that shuffles items in a collection. + /// + /// Generic type parameter. + /// The @this to act on. + public static void Shuffle(this IList @this) + { + int length = @this.Count; + for (int i = 0; i < length; i++) + { + int randomIndex = i + _random.Next(length - i); + T item = @this.ElementAt(randomIndex); + @this[randomIndex] = @this[i]; + @this[i] = item; + } + } +} \ No newline at end of file diff --git a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs index 0cd404ba..84bb255b 100644 --- a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs +++ b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs @@ -10,7 +10,7 @@ public static partial class Extensions { /// - /// An ICollection<T> extension method that swaps item only when it exists in a collection. + /// An IList<T> extension method that swaps item only when it exists in a collection. /// /// Generic type parameter. /// The @this to act on. diff --git a/src/Z.Collections/Z.Collections.csproj b/src/Z.Collections/Z.Collections.csproj index ccd0c9d0..32f462e0 100644 --- a/src/Z.Collections/Z.Collections.csproj +++ b/src/Z.Collections/Z.Collections.csproj @@ -63,6 +63,7 @@ + diff --git a/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs b/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs new file mode 100644 index 00000000..71cf5225 --- /dev/null +++ b/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs @@ -0,0 +1,44 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Collections.Test +{ + [TestClass] + public class System_Collections_Generic_IList_T_Shuffle + { + [TestMethod] + public void Shuffle() + { + // Input + var intList = new List() { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; + + // Examples + var example1 = new List(intList); + var example2 = new List(intList); + var example3 = new List(intList); + var example4 = new List(intList); + var example5 = new List(intList); + + //Shuffle and pack results + example1.Shuffle(); + example2.Shuffle(); + example3.Shuffle(); + example4.Shuffle(); + example5.Shuffle(); + + var shuffledResults = new List> {example1, example2, example3, example4, example5}; + + // Unit Test + Assert.IsTrue(shuffledResults.All(e=> e.Count() == 20), "The shuffle should't change the collection length"); + Assert.IsFalse(shuffledResults.Contains(intList), "The input collection should not be contained in shuffled results"); + Assert.IsTrue(shuffledResults.Distinct().Count() == shuffledResults.Count, "The shuffled results should be unique"); + } + } +} \ No newline at end of file diff --git a/test/Z.Collections.Test/Z.Collections.Test.csproj b/test/Z.Collections.Test/Z.Collections.Test.csproj index 5cdf1ca3..78b91e22 100644 --- a/test/Z.Collections.Test/Z.Collections.Test.csproj +++ b/test/Z.Collections.Test/Z.Collections.Test.csproj @@ -78,6 +78,7 @@ +