Randomly choose an array C# [closed]

2019-09-22 13:50发布

问题:

I have 6 string arrays that contain a series of characters. What I would like to be able to do is random pick an array and once its picked afterwards, will produce a final string response. Do I need a list to do this or is there another way?

All of this is just an small array exercise using a random variable.

I should specify that this is a console application.

回答1:

A simple option would be: Make an array of the arrays. Pick a random index to get one of the arrays. Create a reference to this and then pick each member as needed.



回答2:

        //let say u have an array of string
        string[] myarr = new string[] { "str1", "str3", "str3", "str4", "str5", "str6"};

        Random rnd = new Random();
        // you dont need a list, simply pick one rnd element from array
        string myRandomPickedString = myarr[rnd.Next(0, myarr.Length - 1)];


标签: c# arrays random