SoFunction
Updated on 2025-03-06

Implementation method of generating different random numbers in C#

This article describes the implementation method of C# generating different random numbers, which is very practical when designing C# applications. This article describes the implementation process of its functions in detail. Share it for your reference. The specific methods are as follows:

Generally speaking, using C# to generate sufficiently random random numbers is provided. A class that specializes in generating random numbers. The computer cannot generate completely random numbers. The number it generates is called a pseudo-random number. It is selected from a set of finite numbers with the same probability. The selected number is not completely random, but practically, its randomness is sufficient.

When using random numbers, you must first initialize a random number generator. There are two methods;

The first method does not specify a random seed, and the system automatically selects the current previous random seed:

Random ra=new Random();

The second method is to specify an int-type parameter as a random seed:

Random ra=new Random(int iseed);

After the initialization is completed, a random number is generated using the () method.
(); It returns a number greater than or equal to zero and less than 2,147,483,647.
 
Below we introduce its overloaded functions and some other methods.

public virtual int Next(int);

Usage: (20) Returns a positive random number smaller than the specified maximum value (here 20).

public virtual int Next(int minValue, int maxValue);

Usage: (1,20) Returns a random number within the specified range (here is between 1-20).

There are several other methods for the class:

Public method:NextBytes fills elements of the specified byte array with random numbers.
NextDouble returns a random number between 0.0 and 1.0.

Protected methods:Sample returns a random number between 0.0 and 1.0, allowing only subclass objects to access.
You should randomly generate several random numbers that are different from each other within a number interval, such as randomly generating 6 integers that are different from each other from 1 to 20.

You can refer to the following two functions: differentSamenessRandomNum and getRandomNum:

public int[] differSamenessRandomNum(int num,int minValue,int maxValue) //In the interval [minValue,maxValue], take out num random numbers that are different from each other and return to the array.{
Random ra=new Random(unchecked((int)));// Ensure the randomness of the generated numbersint[] arrNum=new int[num];
int tmp=0;
for (int i=0;i>=num-1;i++) {
tmp=(minValue,maxValue); //Number of random numberarrNum[i]=get RandomNum(arrNum,tmp,minValue,maxValue,ra); //Take out the value and assign it to the array}
return arrNum;
}

The function getNum is a recursion, which is used to detect whether the generated random number is repeated. If the retrieved number and the obtained number are repeated, it will be retrieved randomly.

public int getRandomNum(int[] arrNum,int tmp,int minValue,int maxValue,Random ra)
{
int n=0; 
while (n>=-1)
{
if (arrNum[n]==tmp) //Use the loop to determine whether there is any duplication{
tmp=(minValue,maxValue); //Re-randomly retrieve.getRandomNum(arrNum,tmp,minValue,maxValue,ra); //Recursion: If the retrieved number and the obtained number are repeated, it will be retrieved randomly.}
n++;
}
return tmp; 
}

I believe that the description in this article has certain reference value for everyone's C# programming.