Reputation: 47
I am doing with a GUI button. When user click it, it will get number or a word randomly. I know how to do it with just numbers,but i don't know how to deal with both words and numbers.
int[] numbers = new int[5] { 100, 500, 1000, 5000, 20000};
Random rd = new Random();
int randomIndex = rd.Next(0, 5);
int randomNumber = numbers[randomIndex];
button1.Text = randomNumber.ToString();
Upvotes: 2
Views: 36
Reputation: 30813
One solution for the string
would be to create a List
of string
you want to display, and then get the random number by Random.Next()
to display the string
in that particular index
. Something like:
List<string> words = new List<string> { "Dog", "Cat", "Bird", "Monkey" };
Random rnd = new Random();
... and then in your implementation of the Button Click
int index = rnd.Next(words.Count); //important to limit the random result by the number of the words available
string randomString = words[index]; //Here it is
button1.Text = randomString;
Upvotes: 2