tags:

views:

380

answers:

3

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
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
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.
True, but reading the OP's questions I thought he meant concatenating the list into a single string variable.
Turnkey
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