Everyone may have used Chinaren's alumni, and it has added a method of preventing irrigation on its message board, which is a picture that consists of random numbers and letters each time. Each message must be imported correctly. The result is generated, otherwise you cannot add a message. This is a good way to prevent malicious attacks, and the core technology is how to generate random numbers. The Chinaren website is implemented using PHP, and we can make full use of the powerful features of ASP.NET easy.
In the .NET Framework, a class system.random specifically used to generate random numbers must be imported into the system namespace when using this class. Of course, namespace system is automatically imported in each ASP.NET page, so we can use this class directly.
For random numbers, everyone knows that the computer is not possible to produce a complete random number, so-called random number generators are complex complicated computing by certain algorithms, using the resulting results to approximate The simulation is completely random number, which is called a pseudo-random number. The pseudo-random number is selected from a limited number of numbers with the same probability. The selected number does not have a complete randomness, but from a practical point of view, its randomness is sufficient. The choice of pseudo-random number is from the random seed, so the selection of random seeds is very important to ensure that the number of pseudo randoms received each time is "random". If the random seed is the same, the random number generated by the same random number generator will be the same. Generally, we use the parameters related to the system time as a random seed, which is also a method of using the random number generator in .NET Framework defaults to the default.
We can initialize a random number generator in two ways:
The first method does not specify a random seed, the system automatically selects the current time as a random seed:
.Random ro = new random ();
The second method can specify an INT type parameter as a random seed:
IT ISEED = 10;
Random ro = new random (10);
After that, we can use this Random class object to generate random numbers, this time you want to use the random.next () method. This method uses quite flexible, you can even specify the upper and lower limits of the generated random number.
The use of the upper limit of the upper limit is as follows:
Int IRESULT;
IRESULT = RO.NEXT ();
The following code specifies the random number of less than 100:
Int IRESULT;
IT IUP = 100;
IRESULT = RO.NEXT (IUP);
And the following code specifies that the return value must be within 50-100:
Int IRESULT;
IT IUP = 100;
IT IDOWN = 50;
IRESULT = RO.NEXT (IDOWN, IUP);
In addition to the random.next () method, the Random class also provides a random.nextdouble () method to generate a random double-precision floating point number between 0.0-1.0:
Double Dresult;
DRESULT = RO.NEXTDOUBLE ();
Another method similar to the random.nextdouble () method is Random.Sample (), which is the only difference between the random.nextdouble () method in the access level, we can see their original statement:
Protected Virtual Double Sample ();
Public Virtual Double Nextdouble ();
Therandom.SAMPLE () method is to protect the method, only object access of the subclass, and the random.sample () method can be seen as an open version of Random.Sample (). Generally, the user rewrites the SAMPLE () method in the subclass of Random to obtain a more general distribution. In this example, we use the random.next () method to generate random numbers.
The following functions are the core of this example, we use him to generate a random int array:
Private int [] getrandomaray (int LENGTH, INT UP, INT DOWN) {INT IFIRST = 0; int [] r r = new int32 [length]; random RO = new random (length * unchecked ((int) DateTime.now.ticks )); ifirst = ro.next (up, down); RTARRAY [0] = IFirst; for (int i = 1; i
The reader may notice that we use a quite troublesome way to generate this random array, why not use the following code? Please look at the following code, here we use the system time as a random seed, continuously acquire two random numbers, and output it:
<% @ Page language = "c #" debug = "true" trace = "false" tracemode = "sortbycategory"%> <% @Import namespace = "system"%>