Get Random Element From List<T> in C#
This demo briefly explains how to get random element from a List<T>
in C#. This example is a ASP.NET console program.
Source Code
Download source code here. This project was created in Visual Studio Community 2019.
Application Demo
Basically, the console simply displays a random string one line at a time. The random part will be either of these three strings: biology-1, biology-2, and biology-3.
The Complete Program
static void Main(string[] args)
{
// Initialize our list
List<string> mylist = new List<string>(new string[] { "biology-1", "biology-2", "biology-3" });
Random R = new Random();
// get random number from 0 to 2.
int someRandomNumber = R.Next(0, mylist.Count());
Console.WriteLine("Hello {0}", mylist.ElementAt(someRandomNumber));
Console.ReadKey();
}
Brief Explanation of the Source Code
I created a list of strings initialized with 3 strings.
List<string> mylist = new List<string>(new string[] { "biology-1", "biology-2", "biology-3" });
To get a random element, what we want to do is use the ElementAt
method of List<T>
, like this,
mylist.ElementAt(someRandomNumber)
Like arrays, a list has elements whose position starts at 0 and ends at mylist.Count() - 1. In this program, we generate the value for someRandomNumber by using the Random.Next method
int someRandomNumber = R.Next(0, mylist.Count());
to get a random number from 0 to mylist.Count()-1. In my example, since I have three elements, I want a random number from 0 to 2. Element #3 does not exist!