Access random item in List in C#
This post will discuss how to access a random item in a List<T> in C#.
1. Using Random#Next() Method
A simple and fairly efficient solution to select a random element from a List involves getting a random index value. The idea is to create an instance of the Random class, and generate a random index between 0 and the total number of elements in the List.
The following solution demonstrates this using the Random#Next() method that returns a non-negative random integer that is less than the specified value. In other words, it returns a random number between 0 and specified value minus one (specified value is exclusive).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> numbers = new List<int>() { 1, 2, 3, 4 }; Random rnd = new Random(); int randIndex = rnd.Next(numbers.Count); int random = numbers[randIndex]; Console.WriteLine(random); } } |
You should not create a new instance of the Random class each time a random value is needed. To reuse the same instance of the Random class to uniformly generate random values, you can create an extension method referring to a static field of the Random class:
|
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 |
using System; using System.Collections.Generic; public static class Extensions { private static Random rnd = new Random(); public static T PickRandom<T>(this IList<T> source) { int randIndex = rnd.Next(source.Count); return source[randIndex]; } } public class Example { public static void Main() { List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 }; int random = numbers.PickRandom(); Console.WriteLine(random); } } |
2. Using OrderBy() Method
Another option is to shuffle the whole list using the OrderBy() method and return either the first item or the last item of the list. This works but is very inefficient. However, This can be used if you require multiple random numbers from a list at once.
|
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 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { static Random rnd = new Random(); public static IEnumerable<T> PickRandom<T>(this IList<T> source, int count) { return source.OrderBy(x => rnd.Next()).Take(count); } } public class Example { public static void Main() { List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 }; var randomNums = numbers.PickRandom(3); Console.WriteLine(String.Join(", ", randomNums)); } } |
You may also use the Fisher-Yates shuffle to shuffle a list in linear time. It does a linear scan of the list and swaps each element with a random element among all remaining elements, including the element itself.
That’s all about accessing a random item in a List<T> in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)