Random Number Generation in C#

Introduction

 
In this blog, I am going to explain the program of Random number generation in C#.
 
As per the Oxford Dictionary, random means: made, done or happening without method or conscious decision. 
 
Random numbers are numbers that occur in a sequence so that two conditions are met: (1) It is impossible to predict future values based on past or present ones, and (2) the values are uniformly distributed over a defined interval or set. Random numbers are important in statistical analysis and probability theory.
 
We will be generating random numbers between two given numbers. Both numbers will be provided by the user along with the count value. Furthermore, random numbers will be generated equal to the count value.
 
For example:
 
Please enter minValue
10
Please enter maxValue
35
How many random numbers do you want?
2
Random numbers between 10 and 35 are:
27
15
 
As seen, random numbers will be generated within the given range.
 
Software Requirements
  • C#
  • Visual Studio or Notepad
Program
  1. using System;  
  2. public class Random {  
  3.     private  
  4.     const int MBIG = int.MaxValue,  
  5.         MAXSEED = 161803398;  
  6.     private int inext, inextp;  
  7.     private readonly int[] SeedArray = new int[56];  
  8.     public Random() {  
  9.         int index, tempSeed1, tempSeed2, seed = Environment.TickCount;  
  10.         seed = (seed == int.MinValue) ? int.MaxValue : Math.Abs(seed);  
  11.         tempSeed1 = MAXSEED - seed;  
  12.         SeedArray[55] = tempSeed1;  
  13.         tempSeed2 = 1;  
  14.         for (int i = 1; i < 55; i++) {  
  15.             index = (21 * i) % 55;  
  16.             SeedArray[index] = tempSeed2;  
  17.             tempSeed2 = tempSeed1 - tempSeed2;  
  18.             if (tempSeed2 < 0) tempSeed2 += MBIG;  
  19.             tempSeed1 = SeedArray[index];  
  20.         }  
  21.         for (int j = 1; j < 5; j++) {  
  22.             for (int k = 1; k < 56; k++) {  
  23.                 SeedArray[k] -= SeedArray[1 + (k + 30) % 55];  
  24.                 if (SeedArray[k] < 0) SeedArray[k] += MBIG;  
  25.             }  
  26.         }  
  27.         inext = 0;  
  28.         inextp = 21;  
  29.     }  
  30.     public int RandomNumber(int minValue, int maxValue) {  
  31.         // condition check  
  32.         if (minValue > maxValue) {  
  33.             // throwing exception when minValue will be greater than maxValue  
  34.             throw new ArgumentOutOfRangeException("minValue should be lower than maxValue""");  
  35.         }  
  36.         // generating range from minValue and maxValue  
  37.         int range = maxValue - minValue;  
  38.         int retVal,  
  39.         locINext = inext,  
  40.             locINextp = inextp;  
  41.         if (++locINext >= 56) locINext = 1;  
  42.         if (++locINextp >= 56) locINextp = 1;  
  43.         retVal = SeedArray[locINext] - SeedArray[locINextp];  
  44.         if (retVal == MBIG) retVal--;  
  45.         if (retVal < 0) retVal += MBIG;  
  46.         // storing values in array  
  47.         SeedArray[locINext] = retVal;  
  48.         inext = locINext;  
  49.         inextp = locINextp;  
  50.         return (int)(retVal * (1.0 / MBIG) * range) + minValue;  
  51.     }  
  52.     static void Main() {  
  53.         int minValue, maxValue, count;  
  54.         Console.WriteLine("Please enter minValue");  
  55.         minValue = int.Parse(Console.ReadLine());  
  56.         Console.WriteLine("Please enter maxValue");  
  57.         maxValue = int.Parse(Console.ReadLine());  
  58.         Console.WriteLine("How many random numbers, Do u want");  
  59.         count = int.Parse(Console.ReadLine());  
  60.         // creating object of the Random Class for accesing the FRandomNumber Method  
  61.         Random random = new Random();  
  62.         Console.WriteLine($ "Random numbers between {minValue} and {maxValue} are");  
  63.         for (int i = 0; i < count; i++) {  
  64.             // calling the RandomNumber Method  
  65.             var output = random.RandomNumber(minValue, maxValue);  
  66.             Console.WriteLine(output);  
  67.         }  
  68.         Console.ReadLine();  
  69.     }  
  70. }  
Now run the code.
 
Output of the first execution:
 
 
 
Output of the second execution:
 
 
 
Note

When another person tests with the same inputs, they will receive a different output, as random numbers cannot produce the same output every time. We will even get different results on the same computer.
 
Thank you so much for reading my blog. In my next blog, we will be talking about checked and unchecked keywords.
Next Recommended Reading Unique Random Number Generation in C#