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
- using System;
- public class Random {
- private
- const int MBIG = int.MaxValue,
- MAXSEED = 161803398;
- private int inext, inextp;
- private readonly int[] SeedArray = new int[56];
- public Random() {
- int index, tempSeed1, tempSeed2, seed = Environment.TickCount;
- seed = (seed == int.MinValue) ? int.MaxValue : Math.Abs(seed);
- tempSeed1 = MAXSEED - seed;
- SeedArray[55] = tempSeed1;
- tempSeed2 = 1;
- for (int i = 1; i < 55; i++) {
- index = (21 * i) % 55;
- SeedArray[index] = tempSeed2;
- tempSeed2 = tempSeed1 - tempSeed2;
- if (tempSeed2 < 0) tempSeed2 += MBIG;
- tempSeed1 = SeedArray[index];
- }
- for (int j = 1; j < 5; j++) {
- for (int k = 1; k < 56; k++) {
- SeedArray[k] -= SeedArray[1 + (k + 30) % 55];
- if (SeedArray[k] < 0) SeedArray[k] += MBIG;
- }
- }
- inext = 0;
- inextp = 21;
- }
- public int RandomNumber(int minValue, int maxValue) {
-
- if (minValue > maxValue) {
-
- throw new ArgumentOutOfRangeException("minValue should be lower than maxValue", "");
- }
-
- int range = maxValue - minValue;
- int retVal,
- locINext = inext,
- locINextp = inextp;
- if (++locINext >= 56) locINext = 1;
- if (++locINextp >= 56) locINextp = 1;
- retVal = SeedArray[locINext] - SeedArray[locINextp];
- if (retVal == MBIG) retVal--;
- if (retVal < 0) retVal += MBIG;
-
- SeedArray[locINext] = retVal;
- inext = locINext;
- inextp = locINextp;
- return (int)(retVal * (1.0 / MBIG) * range) + minValue;
- }
- static void Main() {
- int minValue, maxValue, count;
- Console.WriteLine("Please enter minValue");
- minValue = int.Parse(Console.ReadLine());
- Console.WriteLine("Please enter maxValue");
- maxValue = int.Parse(Console.ReadLine());
- Console.WriteLine("How many random numbers, Do u want");
- count = int.Parse(Console.ReadLine());
-
- Random random = new Random();
- Console.WriteLine($ "Random numbers between {minValue} and {maxValue} are");
- for (int i = 0; i < count; i++) {
-
- var output = random.RandomNumber(minValue, maxValue);
- Console.WriteLine(output);
- }
- Console.ReadLine();
- }
- }
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.