Reputation: 1888
I'm doing a C# coding. In my coding, I need to generate a random list of numbers. Normally, if we create a random, we need to select the range of the number. However for my case, I need to create a random number from an array. Any idea? 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: Each time i click the submit button, a random number of numbers[] will 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. Thanks!
Upvotes: 6
Views: 26285
Reputation: 223207
You can create a random number whihch would represent the index from the array. Access the array element from that random index and you will get your number, since all your numbers in the array seems to be distinct.
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 to @Corak) You can generate random Index based on array length, that would make it dynamic and work for any array's length.
int randomIndex = rd.Next(0, numbers.Length);
Edit 2: (For extra part of question).
You need to maintain a list of unique index numbers at class level. So your code would be something like:
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.
}
Upvotes: 15
Reputation: 23462
I would create a class that would represent an random enumerable and than use then extension methond First
to generate the first index you should use in the array. It would 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();
}
}
Upvotes: 1
Reputation: 3573
You could create a string with all the values combined and then obtain the hashcode of it. This hashcode can then be used to seed the random.
Using the HashCode approach makes the seed more dependent on a data of you 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());
}
Upvotes: 0
Reputation: 7919
Here is a parametric way:
randIndex = rd.Next(0, numbers.Length);
int myNo = numbers[randIndex];
Upvotes: 1
Reputation: 39248
Random r = new Random();
int index = r.Next(0, 5);
int randomNum = numbers[index];
This will give you random numbers between 0 and 4 which can be used to index your array and in turn pull random values from the array
Upvotes: 4
Reputation: 2012
This will create a random and unique number from 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;
}
}
}
Upvotes: 0
Reputation: 2028
You could create a Random number and then use the mod operator % by 5 and use that as the index for your array.
Eg.
modRd = rd % 5; randomNumber = numbers[modRd];
Upvotes: 0
Reputation: 2162
You have to generate array index randomly to get a random number from your array.
just apply the random function to generate numbers between the range of the array index(0, array.length - 1)
Upvotes: 0
Reputation: 11883
Just create a random index as usual, and index into the array with that value instead of returning it directly.
Upvotes: 0