Random selection of two phrases (words)

Tell me if there is any simple method of choosing from two phrases. For example, implementing such a

Random r = new Random();
string stroka ="стакан";
switch(r.Next(2))
{
     case 0:
          stroka += " холодный";
          break;
     case 1:
          stroka += " теплый";
          break;
}

This code is too cumbersome, is there any reduction? For example:

function(glass {warm|cold});

Author: Nick Volynkin, 2012-06-29

4 answers

You can twist it as you like, the first thing that came to mind

string stroka ="стакан";
string[] arr = {"теплый","холодный"};
Console.WriteLine(stroka+" "+arr[new Random().Next(0, arr.Length-1)]);

If you need a function, then you can do this:

public string ArrayRand(String stroka, String[] arr)
{
    return stroka += " "+arr[new Random().Next(0, arr.Length-1)];
}
 1
Author: johniek_comp, 2012-06-29 06:37:43

Here's a one-liner:

var stroka = new Random().Next(2) == 0 ? "стакан холодный" : "стакан теплый";
 4
Author: Tolyandre, 2012-06-29 07:31:44

Here you write (in the comments under the first answer):

Yes, but it is also not convenient, it is cumbersome... imagine if it is not two words, but a whole paragraph, where there are dozens of words that should be substituted randomly. in general, in fact, this is a generator of unique texts.

In this case, I can offer such a solution, you should think about it yourself, because words in a paragraph can be separated by dots, commas, and other punctuation marks. Here's the idea:

 ...
            // наш абзац со словами =)
            string strings = "hot cold red blue white old new green light";
            // и вот мы их разбиваем на словечки, далее - стандартная схема!
            string[] words = strings.Split(' ');
            string mainword = "cap is ";
            Random rand = new Random();
            int num = rand.Next(0, words.Length);
            mainword += words[num];
            MessageBox.Show(mainword);
...
 3
Author: Free_man, 2012-06-29 08:17:18

You can also add extension for the array. For example:

string stroka ="стакан";
string[] arr = {"теплый","холодный"};
Console.WriteLine(arr.Rnd());

And the extension class for the array

public static class ArrayExtensions
    {
        public static string Random(this string[] arr)
        {
            return arr[new Random().Next(0, arr.Length - 1)];
        }
    }

The same thing that johniek_comp suggested, only more convenient to use, imho.

 2
Author: lavrik_dndz, 2012-06-29 07:28:16