2

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...

1
  • So many of the same (right) answers. Can I just have a Dog? I know it's not random but heck, dogs rock. Commented Oct 19, 2010 at 16:18

7 Answers 7

17
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 
Sign up to request clarification or add additional context in comments.

6 Comments

Length minus 1?? string blah = mystrings[rnd.Next(0, mystrings.Length - 1];
No, Random.Next is exclusive of the upper bound. msdn.microsoft.com/en-us/library/2dx6wyd4.aspx
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.
use the same instance of Random each time. every call to .Next() gets you a new random number.
oh, i kept calling new randoms so i kept getting the same random.
|
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];

Comments

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];

Comments

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

Comments

1

This should do it:

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

Comments

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.

Comments

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());

1 Comment

You could do mystrings.OrderBy(s => new Random().Next()).First(); as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.