How to create a random number from an array

I am doing coding in C #. In my coding, I need to create a random list of numbers. Usually, if we create random, we need to select a range of numbers. However, for my case, I need to create a random number from an array. Any ideas? I am using XAML and C #.

private void Submit_Click(object sender, RoutedEventArgs e) { int[] numbers = new int[5] {32, 67, 88, 13, 50}; Random rd = new Random(); //int myNo = rd.Next(numbers[])? } 

EXTRA : every time I press the submit button, a random number of numbers [] is selected. How can I make sure the number is not repeated. For example: 1st click, myNo = 67; 2nd click, myNo = 50; 3rd click, myNo = 88; 4th click, myNo = 32; 5th click, myNo = 13. Thank you!

+4
source share
9 answers

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. } 
+12
source
 Random r = new Random(); int index = r.Next(0, 5); int randomNum = numbers[index]; 

This will give you random numbers from 0 to 4, which you can use to index your array and, in turn, pull random values ​​from the array

+3
source

Here is the parametric way:

 randIndex = rd.Next(0, numbers.Length); int myNo = numbers[randIndex]; 
+1
source

I would create a class that would represent a random enumeration, and then use the metond First extension to generate the first index that you should use in the array. It will look something like this:

 class Program { static void Main(string[] args) { int[] numbers = new int[5] { 32, 67, 88, 13, 50 }; var randomEnumerable = new RandomEnumerable(0, numbers.Length); int i = 0; while (i < numbers.Length) { var nextIndex = randomEnumerable.First(); var number = numbers[nextIndex]; Console.WriteLine(number); i++; } Console.ReadLine(); } } class RandomEnumerable : IEnumerable<int> { private readonly int _max; private readonly int _min; private Random _random; public RandomEnumerable(int min, int max) { _max = max; _min = min; _random = new Random(); } public IEnumerator<int> GetEnumerator() { while (true) { yield return _random.Next(_min, _max); } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } 
+1
source

Just create a random index, as usual, and index into an array with this value, rather than returning it directly.

0
source

You need to create an array index randomly to get a random number from your array.

just use a random function to generate numbers between the index range of the array ( 0, array.length - 1)

0
source

You can create a random number and then use the mod% operator at 5 and use it as an index for your array.

Eg. modRd = rd % 5; randomNumber = numbers[modRd];

0
source

This will create a random and unique number from the array.

  public class Shuffler { Random randomNumber; public Shuffler() { randomNumber = new Random(); } /// <summary> /// Shuffling the elements of Array /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> public void Shuffle<T>(IList<T> array) { for (int n = array.Count; n > 1; ) { int k = randomNumber.Next(n); //returning random number less than the value of 'n' --n; //decrease radom number generation area by 1 //swap the last and selected values T temp = array[n]; array[n] = array[k]; array[k] = temp; } } } 
0
source

You can create a string with all the values ​​combined and get a hash code. This hash code can then be used to plant random.

Using the HashCode approach, the splash screen is more dependent on the data array .

  public static void Main() { int[] numbers = new int[5] { 32, 67, 88, 13, 50 }; StringBuilder valuesToBeHashed = new StringBuilder(); for (int i = 0; i < numbers.Length; i++) { valuesToBeHashed.Append(numbers[i] + ","); } Random rd = new Random(valuesToBeHashed.GetHashCode()); Console.WriteLine("Hashcode of Array : {0} ",valuesToBeHashed.GetHashCode()); Console.WriteLine("Random generated based on hashcode : {0}", rd.Next()); } 
0
source