-12

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.

Karim O.
  • 1,325
  • 6
  • 22
  • 36
  • 1
    Please show what you've tried thus far. You might need a list or some such if you don't want to repeat the same string twice... – Lee Taylor Jan 28 '13 at 20:17
  • 2
    Please read [faq] and [ask] before asking a question. – Soner Gönül Jan 28 '13 at 20:17
  • 1
    I'd probably look into the `Random` class, but maybe that's just me. – Dave Zych Jan 28 '13 at 20:18
  • Please provide some code, maybe it's easier to understand what you would like to achieve. – Alex Filipovici Jan 28 '13 at 20:18
  • 1
    show us what you've tried. We are here to help, not to do the work for you. – Nevyn Jan 28 '13 at 20:19
  • You use [`Random`](http://msdn.microsoft.com/en-us/library/system.random.aspx) to generate a random number, and then use the array that corresponds to that number. See [this answer](http://stackoverflow.com/a/3975307/298754) for an example. – Bobson Jan 28 '13 at 20:18

2 Answers2

4

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.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • here is what I have done to put all of those arrays in one. string[][] options = new string[][]{one, two, three, four, five...}; – Karim O. Jan 28 '13 at 21:05
0
        //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)];
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110