Random numbers are widely used. They can be used to randomly display images. They can be used to prevent bored people from bumping in the Forum and encrypt information. This article discusses how to randomly generate several random numbers in a given number range. For example, randomly generate 6 random integers from 1 to 20, this article introduces the usage of random numbers in Visual C.
. Net. frameword provides a special random generation class system. Random, which is imported by default and can be used directly during programming. We know that 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.
We can use the following two methods to initialize a random number generator;
The first method does not specify a random seed. The system automatically selects a Random Seed before the current time:
Random Ra = new random ();
The second method is to specify an int type parameter as a random seed:
Int iseed = 6;
Random Ra = new random (iseed );
Next we will use the random. Next () method to generate a random number.
RA. Next ();
It returns a number greater than or equal to zero and less than 2,147,483,647, which does not meet our needs. Next we will introduce its overloaded functions and other methods.
Public Virtual int next (INT );
Usage: RA. Next (20)
Returns a positive random number that is less than the specified maximum value (20 here.
Public Virtual int next (INT minvalue, int maxvalue );
Usage: RA. Next (1, 20)
Returns a random number in the specified range (between 1 and 20). This function is used in the following instances.
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 random number between 0.0 and 1.0.
Protected methods:
Sample returns a random number between 0.0 and 1.0, which can only be accessed by subclass objects.
The above describes the basic usage of random numbers. Next we will use an instance for further introduction. 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.
The following two functions are used: getrandomnum and getnum:
Public int [] getrandomnum (INT num, int minvalue, int maxvalue ){
Random Ra = new random (unchecked (INT) datetime. Now. ticks ));
Int [] arrnum = new int [num];
Int TMP = 0;
For (INT I = 0; I <= num-1; I ++ ){
TMP = Ra. Next (minvalue, maxvalue); // random count
Arrnum [I] = getnum (arrnum, TMP, minvalue, maxvalue, RA); // The retrieved value is assigned to the array.
}
Return arrnum;
}
Getrandomnum is the random number of num in the interval [minvalue, maxvalue]. The returned array contains the result.
The random number is the random Ra = new random (unchecked (INT) datetime. now. ticks); why not use random Ra = new random (); (the system automatically selects a Random Seed before the current time?
It is not safe to use system time as random seed. If an application runs on a fast computer, the system clock of the computer may not have time to change between calls of this constructor, the seed values of different random instances may be the same. In this case, we need another algorithm to ensure the randomness of the generated numbers. Therefore, to ensure that the random number is "random", we have to use a more complex method to obtain random seeds. In the above program, we first use the system time as the random seed, and then multiply the random number generated last time with the cyclic variable and an integer Parameter Related to the system time, as a Random Seed, different random seed is obtained each time, and a random number is generated.
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. It is worth noting that a random number instance must be used for generation. Therefore, RA must be passed in to getnum as a parameter. Otherwise, the generated number will be repeated.
Public int getnum (INT [] arrnum, int TMP, int minvalue, int maxvalue, random RA ){
Int n = 0;
While (n <= arrnum. Length-1)
{
If (arrnum [N] = TMP) // use loops to determine whether there are duplicates.
{
TMP = Ra. Next (minvalue, maxvalue); // obtain the result randomly.
Getnum (arrnum, TMP, minvalue, maxvalue, RA); // recursion: If the obtained number is repeated with the obtained number, it is randomly retrieved.
}
N ++;
}
Return TMP;
}