This post will discuss how to randomize a List in C#.

1. Using Enumerable.OrderBy Method

The Enumerable.OrderBy method sorts the elements of a sequence using the specified comparer. We can tweak it as follows to shuffle items in random order by using a random number generator. Note that this creates a shuffled copy of the source list and leaves the source list unchanged.

Download  Run Code

2. Using Loop

To in-place randomize a list, we can write an extension method based on the Fisher–Yates shuffle algorithm. This can be implemented as follows in C#.

Download  Run Code

That’s all about randomizing a List in C#.