DOTNET. frameword provides a special random generation class system. random, a computer cannot generate completely random numbers. The numbers it generates are called pseudo-random numbers. They are selected from a group of finite numbers with the same probability, the selected number is not random, but its degree of randomness is sufficient.
When using a random number, you must first initialize a random number generator. There are two methods;
The first method does not specify a random seed. The system automatically selects a Random Seed before the current time:
Random Ra=NewRandom ();
The second method is to specify an int type parameter as a random seed:
Random Ra=NewRandom (IntIseed );
After initialization, the random number is generated using the random. Next () method.
RA. Next (); it returns a value greater than or equal to zero and less than 2,147,483, The number of 647.
Next we will introduce its overload functions and other methods.
Public Virtual IntNext (Int);
Usage: RA. Next (20)
Returns a positive random number that is less than the specified maximum value (20 here.
Public Virtual IntNext (IntMinvalue,IntMaxvalue );
Usage: RA. Next (1,20)
Returns a value in the specified range (1 in this example ).-A random number between 20.
There are several methods for the system. Random class:
Public method:
Nextbytes uses a random number to fill in the elements of the specified byte array.
Nextdouble returns a value0.0And1.0Between.
Protected methods:
Sample returns a value0.0And1.0The random number between them. Only the sub-class objects can be accessed.
You must randomly generate several random numbers in a given number range. For example, you can randomly generate six random integers from 1 to 20.
You can refer to the following two functions:
Differsamenessrandomnum and getrandomnum:
Public Int[] Differsamenessrandomnum (IntNum,IntMinvalue,IntMaxvalue)
//In the interval [minvalue, maxvalue], retrieve random numbers with different num numbers and return an array.
{
random Ra = New random ( unchecked ( int ) datetime. now. ticks); /// ensure the randomness of the generated numbers
int [] arrnum = New int [num];
IntTMP=0;
For(IntI=0; I<=Num-1; I++)
{
TMP=RA. Next (minvalue, maxvalue );//Random Number
Arrnum [I]=GetRandomnum (arrnum, TMP, minvalue, maxvalue, RA );//The retrieved value is assigned to the array.
}
ReturnArrnum;
}
The getnum function is recursive. It is used to check whether the generated random number is repeated. If the obtained number and the obtained number are repeated, the random number is obtained again.
Public IntGetrandomnum (Int[] Arrnum,IntTMP,IntMinvalue,IntMaxvalue, random RA)
{
IntN=0;
While(N<=Arrnum. Length-1)
{
If(Arrnum [N]=TMP)//Use loops to determine whether there are duplicates
{
TMP=RA. Next (minvalue, maxvalue );//Re-random acquisition.
Getrandomnum (arrnum, TMP, minvalue, maxvalue, RA );
//Recursion: If the obtained number and the obtained number have already been repeated, they are randomly retrieved again.
}
N++;
}
ReturnTMP;
}