How to pick a random element from a list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main() {
        IList<int> lst = new[] { 4, 6, 8, 1, 9, 0, 7 };

        int num = lst[new Random().Next(lst.Count)];
        
        Console.WriteLine(num);
    }
}




/*
run:

9

*/

 



answered Jan 6, 2023 by avibootz
edited Jan 7, 2023 by avibootz
...