Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IList<T> Shuffle extension #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();

/// <summary>
/// An IList&lt;T&gt; extension method that shuffles items in a collection.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="this">The @this to act on.</param>
public static void Shuffle<T>(this IList<T> @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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public static partial class Extensions
{
/// <summary>
/// An ICollection&lt;T&gt; extension method that swaps item only when it exists in a collection.
/// An IList&lt;T&gt; extension method that swaps item only when it exists in a collection.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="this">The @this to act on.</param>
Expand Down
1 change: 1 addition & 0 deletions src/Z.Collections/Z.Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="System.Collections.Generic.IList[T]\IList[T].Shuffle.cs" />
<Compile Include="System.Collections.Generic.IList[T]\IList[T].Swap.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].AddIf.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].AddIfNotContains.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<int>() { 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<int>(intList);
var example2 = new List<int>(intList);
var example3 = new List<int>(intList);
var example4 = new List<int>(intList);
var example5 = new List<int>(intList);

//Shuffle and pack results
example1.Shuffle();
example2.Shuffle();
example3.Shuffle();
example4.Shuffle();
example5.Shuffle();

var shuffledResults = new List<List<int>> {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");
}
}
}
1 change: 1 addition & 0 deletions test/Z.Collections.Test/Z.Collections.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].RemoveRangeIf.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].RemoveRangeIfContains.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].RemoveWhere.cs" />
<Compile Include="System.Collections.Generic.IList[T]\IList[T].Shuffle.cs" />
<Compile Include="System.Collections.Generic.IList[T]\IList[T].Swap.cs" />
<Compile Include="System.Collections.Generic.IDictionary[string, object]\IDictionary[string, object].ToExpando.cs" />
<Compile Include="System.Collections.Generic.IDictionary[string, string]\IDictionary[string, string].ToNameValueCollection.cs" />
Expand Down