1

i basically just want to pick a random value in a string split by '|'. I can't find a good example does anybody have an idea?

string[] mystrings = ("apple|orange|mayo|fruit|dog"):

string blah = "here i am "+resultsofrandom+" result chosen from mystring was " resultofrandom

obviously string blah is just an example, i just want the random chosen string from mystrings back into a new string...

7 Answers7

16
string[] mystrings = "apple|orange|mayo|fruit|dog".Split('|');
Random rnd = new Random();
string blah1 = mystrings[rnd.Next(mystrings.Length)];
string blah2 = mystrings[rnd.Next(mystrings.Length)];
string sentence = "here i am " + blah1 + " result chosen from mystring was " + blah2 
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • 1
    Length minus 1?? `string blah = mystrings[rnd.Next(0, mystrings.Length - 1];` – Brad Oct 19 '10 at 16:10
  • 8
    No, Random.Next is exclusive of the upper bound. http://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx – John Sheehan Oct 19 '10 at 16:11
  • How do I randomly choose a new value for Random each time, like if i want to do blah = int choice = new Random().Next(randommakework.Length); and then string blah2 = mystrings[rnd.Next(mystrings.Length)]; (but this is a new random. –  Oct 19 '10 at 17:12
  • use the same instance of Random each time. every call to .Next() gets you a new random number. – John Sheehan Oct 19 '10 at 17:26
  • oh, i kept calling new randoms so i kept getting the same random. –  Oct 19 '10 at 17:31
  • here's an explanation for why that's happening: http://stackoverflow.com/questions/1654887/random-next-returns-always-the-same-values – John Sheehan Oct 19 '10 at 17:45
6

You could do this rather simply by splitting the string:

string[] mystrings = "apple|orange|mayo|fruit|dog".Split('|');

Then use a the Random class to pick one of those strings:

int choice = new Random().Next(mystrings.Length);

Now you can put it together:

string blah = "Your selection is: " + mystrings[choice];
John Fisher
  • 22,355
  • 2
  • 39
  • 64
2
Random rnd= new Random();
        int baseZeroArrayLen = 0;
        string[] mystrings = ("apple|orange|mayo|fruit|dog").Split('|');
        baseZeroArrayLen = mystrings.Length - 1; 
        int randomNumber = rnd.Next(baseZeroArrayLen);
        string rndString = mystrings[randomNumber];
vaitrafra
  • 666
  • 3
  • 15
1
var mystrings = ("apple|orange|mayo|fruit|dog").Split('|');
string blah = "here i am " + mystrings[new Random().Next(0, mystrings.Length)] + " result chosen..";

I think it will work as expected

Onkelborg
  • 3,927
  • 1
  • 19
  • 22
1

This should do it:

string[] mystrings = ("apple|orange|mayo|fruit|dog").Split('|');
        Random randomInt = new Random();
            string blah = mystrings[randomInt.Next(mystrings.Length)];
Brandon
  • 11
  • 1
0

Use String.Split() to split up the delimited string and store each separate value in a string array. Then randomly pick an index into that array and display the corresponding string.

Bernard
  • 7,908
  • 2
  • 36
  • 33
0

Completely unnecessary LINQ alternative. Although the string.Format might be nice here.

string[] mystrings = "apple|orange|mayo|fruit|dog".Split('|');

string blah = string.Format("here i am {0} result chosen from mystring was {0}",
               mystrings.Skip(new Random().Next(mystrings.Length)).First());
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155