mardi 22 novembre 2022

How can I generate a random string, out of a pre prepared list of strings in C#?

I need to generate a random string from a pool of 6 pre-generated strings. I created a list, but I don't understand how to use "random" to generate one random string out of that list.

This is what I have so far:

string RandomPowerSentence()
{
    Random rnd = new Random();
  
    List<string> powerStrings = new List<string>()
    {
        "You are kind!",
        "You are beautiful!",
        "You are smart!",
        "You are awesome!",
        "You are funny!",
        "You're a f*cking A!"
    };

    //I assume that here I put the code that generates a random string out of the list with rnd
    

    return  "the random string from the list";

Help will be very appreciated!

I used the random class, but I don't understand/know how to use it with strings and not ints.




Aucun commentaire:

Enregistrer un commentaire