How to Shuffle a List in C# - Step by Step Examples
How to Shuffle a List in C# ?
Answer
To shuffle a list in C#, you can use the OrderBy
method with a random key selector or implement a custom shuffle method using the Fisher-Yates algorithm.
✐ Examples
1 Shuffle a List Using the OrderBy Method
In this example,
- We create a list named
numbers
with integer values. - We use the
OrderBy
method with a random key selector to shuffle the list. - The
OrderBy
method sorts the elements based on random keys generated byGuid.NewGuid()
. - Finally, we print the shuffled list to standard output.
C# Program
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
List<int> shuffledNumbers = numbers.OrderBy(x => Guid.NewGuid()).ToList();
Console.WriteLine("Shuffled list: " + string.Join(", ", shuffledNumbers));
}
}
Output
Shuffled list: 3, 1, 5, 4, 2
2 Shuffle a List Using the Fisher-Yates Algorithm
In this example,
- We create a list named
numbers
with integer values. - We implement a custom method called
ShuffleList
that takes a list as input. - Inside
ShuffleList
, we use a random number generator to swap each element with a random element that comes after it, iterating from the end of the list to the start. - The outer loop iterates from the last element to the second element, and the inner loop performs the element swap.
- Finally, we print the shuffled list to standard output.
C# Program
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
ShuffleList(numbers);
Console.WriteLine("Shuffled list: " + string.Join(", ", numbers));
}
static void ShuffleList(List<int> list)
{
Random rng = new Random();
int n = list.Count;
for (int i = n - 1; i > 0; i--)
{
int j = rng.Next(i + 1);
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
Output
Shuffled list: 4, 1, 5, 2, 3
Summary
In this tutorial, we learned How to Shuffle a List in C# language with well detailed examples.
More C# Lists Tutorials
- How to create an Empty List in C# ?
- How to Initialize a List in C# ?
- How to Get Length of a List in C# ?
- How to create a List of size N in C# ?
- How to create a List of Numbers from 1 to N in C# ?
- How to create a List of Strings in C# ?
- How to create a List of Empty Lists in C# ?
- How to Access Elements in a List in C# ?
- How to get Element in a List at a Specific Index in C# ?
- How to get First Element in a List in C# ?
- How to get Last Element in a List in C# ?
- How to Iterate Over a List in C# ?
- How to Iterate Over a List with Index in C# ?
- How to Iterate Over a List in Reverse Order in C# ?
- How to check if a List is Empty in C# ?
- How to check if a List is Not Empty in C# ?
- How to get Sub List in C# ?
- How to get the Index of Specified Element in a List in C# ?
- How to check if a Specific Element is present in the List in C# ?
- How to check if a List contains all the elements of Another List in C# ?
- How to count the Number of Occurrences of Specific Element in the List in C# ?
- How to find the Element with Maximum Number of Occurrences in a List in C# ?
- How to find the Element with Minimum Number of Occurrences in a List in C# ?
- How to Sort a List in C# ?
- How to Sort a List in Ascending Order in C# ?
- How to Sort a List in Descending Order in C# ?
- How to create a Two Dimensional List in C# ?
- How to Iterate over a Two Dimensional List in C# ?
- How to create a Three Dimensional List in C# ?
- How to Copy a List in C# ?
- How to deep Copy a List in C# ?
- How to Split a List in C# ?
- How to Join Lists in C# ?
- How to Append an Element to a List in C# ?
- How to Insert an Element at Specific Index in a List in C# ?
- How to Append a List to another List in C# ?
- How to Concatenate Two Lists in C# ?
- How to check if Two Lists are Equal in C# ?
- How to check if Two Lists have Same Elements (Regardless of Order) in C# ?
- How to Convert a List of Integers to a List of Strings in C# ?
- How to Convert a List of Strings to a List of Integers in C# ?
- How to Convert a List of Floats to a List of Strings in C# ?
- How to Convert a List of Strings to a List of Floats in C# ?
- How to Reverse a List in C# ?
- How to Shuffle a List in C# ?
- How to Slice a List in C# ?
- How to Slice First N Elements from a List in C# ?
- How to Slice Last N Elements from a List in C# ?
- How to Rotate Elements in a List in C# ?
- How to Filter Elements of a List based on a Condition in C# ?
- How to Remove Duplicates in a List in C# ?
- How to Remove Element at a Specific Index from a List in C# ?
- How to Remove Specific Element from a List in C# ?
- How to Remove Element from List based on a Condition in C# ?
- How to Sort a List of Strings in Dictionary Order in C# ?
- How to Concatenate Strings in List in C# ?
- How to create a List of Lists in C# ?
- How to create a List of Dictionaries in C# ?
- How to create a List of Sets in C# ?