In the following examples you will find more than one solution to mix a list. Some samples were taken directly from the internet addresses. And You can use the following codes for to shuffle numbers, strings and object.
Note: The internet adresses were added at the end of the article.
Solution 1: Solving the problem in the same class with for statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
class Program { public static List<T> Shuffle<T> (List<T> list) { Random rnd = new Random(); for (int i = 0; i < list.Count; i++) { int k = rnd.Next(0, i); T value = list[k]; list[k] = list[i]; list[i] = value; } return list; } static void Main(string[] args) { List<int> mylist = new List<int>(); mylist.Add(1); mylist.Add(2); mylist.Add(3); mylist.Add(4); mylist.Add(5); mylist.Add(6); mylist = Shuffle(mylist); foreach (var item in mylist) { Console.WriteLine(item); } Console.ReadLine(); } } |
Solution 2: Shuffing using by Linq
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Program { static void Main(string[] args) { List<int> mylist = new List<int>(); mylist.Add(1); mylist.Add(2); mylist.Add(3); mylist.Add(4); mylist.Add(5); mylist.Add(6); //shuffle var rnd = new Random(); var result = mylist.OrderBy(item => rnd.Next()); foreach (var item in result) { Console.WriteLine(item); } Console.ReadLine(); } } |
Solution 3: C# Shuffle List of Objects
Student Class
1 2 3 4 5 6 7 8 9 |
public class Student { string name; public Student(string name) { Name = name; } public string Name { get => name; set => name = value; } } |
Main Method: Shuffle with Linq
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class Program { static void Main(string[] args) { List<Student> mylist = new List<Student>(); mylist.Add(new Student("JAMES")); mylist.Add(new Student("DAVID")); mylist.Add(new Student("PAUL")); mylist.Add(new Student("GEORGE")); mylist.Add(new Student("LARRY")); //shuffle var rnd = new Random(); var result = mylist.OrderBy(item => rnd.Next()); foreach (var item in result) { Console.WriteLine(item.Name); } Console.ReadLine(); } } |
Solution 4 : Create an extension method and shuffle by itself (*)
1- Add new class for extetions
2- Add Shuffle method into MyExtentions class as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
static class MyExtensions { public static void Shuffle<T>(this IList<T> list) { Random rng = new Random(); int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } } } |
3-Usage in Main method
1 2 3 4 |
//shuffle mylist.Shuffle(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Program { static void Main(string[] args) { List<int> mylist = new List<int>(); mylist.Add(1); mylist.Add(2); mylist.Add(3); mylist.Add(4); mylist.Add(5); mylist.Add(6); //shuffle mylist.Shuffle(); foreach (var item in mylist) { Console.WriteLine(item); } Console.ReadLine(); } } |
Source:
- (*)https://stackoverflow.com/questions/273313/randomize-a-listt