From the course: Advanced C#: Functional Programming Patterns
Unlock this course with a free trial
Join today to access over 24,500 courses taught by industry experts.
Randomize the order of a list - C# Tutorial
From the course: Advanced C#: Functional Programming Patterns
Randomize the order of a list
We can use our new gen class to shuffle the contents of a sequence. In other words, use LINQ OrderBy() and the Gen.Next() method to reorder the list based on random numbers. So I'm doing that here in this OrderListRandomly() method. Let's take a look at the first two lines of code. I'm creating an immutable list that has numbers one through nine, and then I'm calling my gen class the ReorderSet() method. This is generic. So I'm specifying that I'm passing in an immutable list of integers. That's what I'm passing in here. And I'll get back a shuffled list of integers. Now because I made this generic, this will work with any immutable list of any type. So the second example I've got down here is using a custom class called robot, has a robot name, and a robot team name. So what I've done here on line 69 through 74 is I've created six robots, added them to this ImmutableList<robot>, and then calling ReorderSet(). The code is identical. The only difference is I use robot for the type, and…