Created
February 6, 2013 12:55
-
-
Save AlexanderBrevig/4722354 to your computer and use it in GitHub Desktop.
Extension method for IList<T> for shuffling the content (not thread safe)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
public static class ListShuffle | |
{ | |
public static void Shuffle<T>(this IList<T> list) | |
{ | |
Random rand = new Random(); | |
int i = list.Count; | |
while (i > 1) { | |
--i; | |
int j = rand.Next(i + 1); | |
T val = list[j]; | |
list[j] = list[i]; | |
list[i] = val; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment