private static readonly Random random = new();
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter a set of comma separated name here:");
var names = Console.ReadLine();
if (string.IsNullOrWhiteSpace(names))
{
Console.WriteLine("Bad name input, try again \n");
continue;
}
Console.WriteLine("Enter number of randomly selcted named:");
var count = Console.ReadLine();
if (!int.TryParse(count, out var nameCount))
{
Console.WriteLine("Bad name count input, try again \n");
continue;
}
// Split the names.
var nameCollection = names.Split(',', StringSplitOptions.TrimEntries).ToList();
// Shuffle names.
nameCollection = nameCollection.OrderBy(_ => random.Next()).ToList();
Console.WriteLine($"Your randomly selected {nameCount} names are : {string.Join(", ", nameCollection.Take(nameCount))}\n\n");
}
}