How to pick numbers at random from a given set of numbers in c#
The Random class defined in the .NET Framework provides functionality to generate pseudo-random number. According to MSDN, the current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random.
However, current implementation does not provide the following two functionalities.
- Picking random numbers from a given set of numbers: For example, you want random number from the given set of numbers. You have a set of numbers like (2, 4, 13, and 21).You would like to pick one number at a time at random from the list.
- Picking random numbers from a range of numbers with no repetition: Current implementation return random numbers within a range with some duplicate number. For example, you want random number from a range of numbers like (4 to 21). You would like to pick one number at a time at random from that range without any repetition or duplicate
However, these two requirements are easy to implement by using the Random class of the .NET Framework. I needed these two functionalities for one of my project, so I wrote a class to implement these two features. I think that this is a trivial work but I am posting it because it can save some of your time. To implement these two functionalities following class named RandomNumberFromAGivenSetOfNumbers is written.
1: class RandomNumberFromAGivenSetOfNumbers
2: {
3: List<int> _setOfNumbers = new List<int>();
4:
5: public List<int> SetOfNumbers
6: {
7: get { return _setOfNumbers; }
8: set { _setOfNumbers = value; }
9: }
10: Random _random = new Random();
11:
12: public RandomNumberFromAGivenSetOfNumbers()
13: {
14:
15: }
16: public RandomNumberFromAGivenSetOfNumbers(int min, int max)
17: {
18: for (int i = min; i <= max; i++)
19: {
20: _setOfNumbers.Add(i);
21: }
22: }
23:
24: public int Next()
25: {
26: if (_setOfNumbers.Count > 0)
27: {
28: int nextNumberIndex = _random.Next(_setOfNumbers.Count);
29: int val = _setOfNumbers[nextNumberIndex];
30: _setOfNumbers.RemoveAt(nextNumberIndex);
31: return val;
32: }
33: return -1;
34: }
35: }
The used code for testing these two features is in the following
1: static void Main(string[] args)
2: {
3: Console.WriteLine("Picking random numbers from a range of numbers with no repetition(Range is 0-24)");
4: Console.WriteLine();
5: RandomNumberFromAGivenSetOfNumbers rdm = new RandomNumberFromAGivenSetOfNumbers(0, 24);
6: string str = string.Empty;
7: for (int i = 0; i < 25; i++)
8: {
9: str += rdm.Next().ToString() + " ";
10: }
11: Console.WriteLine(str);
12: Console.WriteLine();
13: Console.WriteLine();
14:
15:
16: Console.WriteLine("Picking random numbers from a given set of numbers( 2, 4, 13, and 21)");
17: Console.WriteLine();
18: RandomNumberFromAGivenSetOfNumbers rdm1 = new RandomNumberFromAGivenSetOfNumbers();
19: rdm1.SetOfNumbers.Add(2);
20: rdm1.SetOfNumbers.Add(4);
21: rdm1.SetOfNumbers.Add(13);
22: rdm1.SetOfNumbers.Add(21);
23: string str1 = string.Empty;
24: for (int i = 0; i < 4; i++)
25: {
26: str1 += rdm1.Next().ToString() + " ";
27: }
28: Console.WriteLine(str1);
29:
30: Console.ReadLine();
31: }
The output of the program is in the following:
You can download the sample code from here. This is a very trivial work, but I hope this will save some of your time.