[Solved] Generate random string from list of string in C#? [closed]


Try this:

class Program
{
    static void Main(string[] args)
    {
        int myRandomIndex = 0;
        var myList = new List<string>(new[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" });
        var results = new List<string>();
        var r = new Random(DateTime.Now.Millisecond);
        for (int ii = 0; ii < 4; ii++)
        {
            myRandomIndex = r.Next(myList.Count);
            results.Add(myList[myRandomIndex]);
            myList.RemoveAt(myRandomIndex);
        }

        Console.WriteLine(string.Join("", results));
        Console.ReadKey();
    }
}

To keep each chosen string unique (prevent duplicates) I remove it from the source list as it is used. You should also do a myList = myList.Distinct() before using the list, to make sure you don’t have duplicates in it to begin with.

0

solved Generate random string from list of string in C#? [closed]