Randomly choose an array C# [closed]

2019-09-22 13:40发布

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.

标签: c# arrays random
2条回答
再贱就再见
2楼-- · 2019-09-22 14:20

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.

查看更多
Melony?
3楼-- · 2019-09-22 14:30
        //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)];
查看更多
登录 后发表回答