All right, so basically all I want is for a bunch of words that I input into a string(would I use one?) and then a random one would be outputted in a TextBox. So, I'd have a list of words (let's say 100 words) and then I'd make it randomly put out 1 of those 100 words in a TextBox. Is this possible? Thanks!
+5
A:
seeselect a random value from an array. here's one of their code samples:
// Initialize the string array
string[] strStrings = { "Random string", "Another random value from the array", "Randomly selected index" };
// Choose a random slogan
Random RandString = new Random();
// Display the random slogan
txtRandom.Text = strStrings[RandString.Next(0, strStrings.Length)];
After you have a random string from your list, you just set it as the value of the text-box. :D
CrazyJugglerDrummer
2010-03-01 01:03:41
A:
I don't think you'd want to use a string but rather a generic list of strings. Then use the random method to return a number between 0 and 99 inclusive, and then use the random number to access the list index.
Turnkey
2010-03-01 01:06:03
If this is the extend of the requirements, using a generic List<string> doesn't add anything that string[] isn't already giving you.
Eric J.
2010-03-01 01:09:13
True, but reading the OP's questions I thought he meant concatenating the list into a single string variable.
Turnkey
2010-03-07 23:49:38
A:
Another way to do it, in addition to CrazyJugglerDrummer's solution, is to use the modulus function in conjunction with the current Ticks.
string[] strings = { "String1", "String2", "String3" }; int position = DateTime.Now.Ticks % strings.Length; string randomString = strings[position];
Steve Danner
2010-03-01 01:30:02