using System; int[] numbers = { 1, 2, 3, 4, 5 }; Random rand = new Random(); int index = rand.Next(numbers.Length); Console.WriteLine($"Random element: {numbers[index]}");
using System; using System.Linq; string[] names = { "Alice", "Bob", "Charlie", "David" }; Random rand = new Random(); string name = names.OrderBy(x => rand.Next()).First(); Console.WriteLine($"Random name: {name}");In this example, we declare a string array with 4 elements. We use the OrderBy() method from the LINQ library to shuffle the array using the Random.Next() method as the sort key. We then retrieve the first element in the shuffled array and display it on the console. Package/Library: The Random class is part of the System namespace in the .NET Framework Class Library. No additional package or library is required to use this class.