You can create a random number that will represent an index from an array. Access the element of the array from this random index and you will get your number, since all your numbers in the array seem different.
int[] numbers = new int[5] { 32, 67, 88, 13, 50 }; Random rd = new Random(); int randomIndex = rd.Next(0, 5); int randomNumber = numbers[randomIndex];
EDIT: (Thanks @Corak ) You can generate a random index based on the length of the array, which will make it dynamic and will work for any length of the array.
int randomIndex = rd.Next(0, numbers.Length);
Edit 2: (For an additional part of the question).
You need to keep a list of unique index numbers at the class level. So your code would look something like this:
Random rd = new Random(); //At class level List<int> uniqueIndices = new List<int>(); //At class level private void Submit_Click(object sender, RoutedEventArgs e) { int[] numbers = new int[5] {32, 67, 88, 13, 50}; int randomIndex = rd.Next(0, numbers.Length); while(list.Contains(randomIndex)) //check if the item exists in the list or not. { randomIndex = rd.Next(0, numbers.Length); } list.Add(randomInex); //...rest of your code. }
source share