Abstract Pseudo-random numbers are widely used in computer software design. This article introduces the general principles of pseudo-random number generation, and the use of the Random class and its methods provided in the ASP.NET Framework to generate random numbers in various ranges that meet various requirements. Finally, combined with the Web control form, the application of random numbers in ASP.NET in software design is explained.
Keywords ASP.NET; pseudo-random number generation; Web; Random-like
random numbers are widely used in software design, especially in the fields of practical environment simulation and testing. In order to pursue truly random sequences, people have used many primitive physical methods to generate uniformly distributed sequences that meet the accuracy (number of bits) within a certain range. Their shortcomings are: slow speed, low efficiency, requiring a large amount of storage space, and being unavailable. Reappear etc. In order to meet the needs of computer simulation research, people turn to research on using algorithms to generate pseudo-random sequences that simulate various probability distributions. Pseudo-random numbers refer to random numbers generated by mathematical recursion formulas. From a practical point of view, the simplest and most natural way to obtain such numbers is to use a random number generator provided by a computer language library. Different development environments provide different functions and methods for generating random numbers. Typically, it outputs a pseudorandom variable whose values are uniformly distributed between 0 and 1.
A random number generator is
a method of generating random numbers in a computer, often using the following formula:
The program that uses this formula to generate a sequence of random numbers a1, a2,... from 0 to 65536 is called a 232-step multiplicative harmonic random number generator. Among them, b, c, and d are positive integers, and d is called the seed of the random sequence generated by the formula.
It can be seen from this formula that once the parameters b, c, and d are determined, the random sequence generated is also determined. This sequence that only satisfies randomness to a certain extent is called pseudo-random number.
Below is an example of a random number generator. Among them, the function random_seed allows the user to select the seed of a random number. When the formal parameter d=0, the current time of the system is used as the random number seed; when d≠0, d is selected as the seed; the function random is based on the given seed. , calculate a new seed and generate a new random number in the range of low~high.
#define MULTIPLIER 0x015A4E35L
#define INCREMENT 1
void random_seed(unsigned long d){
if (d==0)seed = time(0);
else seed = d;}
unsigned int random(unsigned long low, unsigned long high)
{
seed = MULTIPLIER * seed + INCREMENT;
return ((seed>>16)% (high-low) + low);
}
}
The pseudo-random number generation mechanism in ASP.NET
makes it impossible for the computer to generate completely random numbers. The so-called random number generator uses a certain algorithm to perform complex operations on pre-selected random seeds, and uses the generated results to approximately simulate completely random numbers. This kind of random number is called a pseudo-random number. Pseudo-random numbers are chosen from a finite set of numbers with equal probability. The numbers chosen are not completely random, but they are random enough for practical purposes.
1. VB.NET built-in functions
ASP.NET programs can use a variety of programming languages. The default programming language is VB.NET. Although VB.NET and VB are somewhat similar, their application environments are different and the methods of writing code are different. There are differences too. In VB.NET, if you want to use mathematical functions, you must pass the Math class provided by the .NET Framework, which is located under the System namespace. Users can add Imports System. Math at the top of the code to use these mathematical functions.
The prototype of the function used to generate random numbers is: Rnd(x). This function is used to generate a single-precision random number between 0 and 1. x is the seed for generating random numbers. If you want to randomly select a number from the range of (min, max), you need to use the formula: random number = (max - min) * Rnd(x) + min.
The selection of pseudo-random numbers starts from random seeds. In order to ensure that the pseudo-random numbers obtained every time are sufficiently "random", the selection of random seeds is very important. If the selected random seeds are the same, the generated random sequences will also be the same. Generally, parameters related to the system time are used as random seeds, which is also the default method used by the random number generator in the .net Framework.
2. Random number class System.Random
Random number class The System.Random class provides the following methods for generating various random numbers that meet different requirements, as shown in Table 1:
Table 1 Various methods provided by the System.Random class
serial number | method name | Function description |
1 | Next() | Returns an integer between 0 and 2147483647 |
2 | Next(i) | Returns an integer between 0 and i |
3 | Next(i,j) | Return an integer between i~j |
4 | Nextdouble() | Returns a random decimal number between 0 and 1 |
5 | Nextdouble(byte()) | Use a random integer between 0 and 255 as the value of each element of the byte array. |
Using the random number class System.Random must be declared first. If you want to use the Nextbytes(byte()) method, you must also declare the byte array before using it.
3. Test program writing
must establish its development and running environment before running the ASP.NET program, including configuring Microsoft IIS and installing .NET Framework SDK and machine MSE editor. The MSE editor is an add-on software for Microsoft Office and is very convenient as an editing tool for ASP.NET. The test program code is as follows:
<script language="vb" runat="server">
sub page_load(send as object,e as eventargs)
dim r1 as random =new random()
response.write("The result of r1.next()is:"+r1.next().tostring())
response.write("Theresultofr1.next(100)is:" +r1.next(100).tostring())
response.write("The result of r1.next(100,150) is:"+r1.next(100,150).tostring())
response.write("The result of r1.nextdouble() is:"+r1.nextdouble().tostring())
r1.nextbytes(r) // dim r(300) as byte
response.write("the r(100) is:")
response.write(r(199))
end sub </script>
The test results are shown in Figure 1:
Figure 1 Pseudo-random number test running results
Example development
application requirements analysis: Create an application for simulating a dice game. In this game, players randomly roll a die. Each die has six sides, representing the six points 1, 2, 3, 4, 5, and 6 respectively. When the die stops, look at the number on the upper surface of the die. If the number of throws is 6, the winning message will be prompted, otherwise the throw will continue.
1. Key technologies
1.1 Web control form
Web control form has the object-oriented characteristics of high-level language. It is an object of the System.Web.UI.WebControls namespace in ASP.NET. The processing process is similar to the HTML control form, with the following characteristics: the form runs on the server side; the input fields are all controls, with powerful and rich properties and methods, and the information of the input fields can be retained; it has the ability to verify the input fields ; Contains data display controls, etc.
A typical Web control form code is as follows:
<asp: button id="sub3" text="confirm" runat="server"/>
Among them, ASP: XXX indicates which type of Web control it is; the ID attribute indicates the identification name of the control; The value of the Runat attribute is server, indicating that this is a control running on the server side.
The Web controls that will be used in this example are mainly button controls. The Image control is used to display image files and has the following attributes: Imageurl, indicating the URL of the image file; Width, the width of the image file display; Height, the height of the image file display, etc.
1.2 Pseudo-random number generation
According to the requirements analysis description, in order to simulate the points obtained by randomly throwing dice, it is necessary to randomly generate integers ranging from 1 to 6. In this case, the Next (i, j) method of the System.Random class can be used , where i=1, j=7. (The author wrote a test program and found that if j=6, the random number generated is between 1 and 5)
2. Program implementation
The main code of the program is as follows:
<script language="vb" runat="server">
sub disp(obj as object,e as eventargs)
dim r1 as random =new random() //Define a random number class
dim file_prefix as string = "Imagesdie"
dim file_suffix as string = ".png"
value = r1. next(1,7) //dim value as Integer
s1 = file_prefix + value.tostring() + file_suffix
pic1.imageurl = s1 //Update the imageurl attribute of the Image control
if value = 6 then
disp.text = "You win" / /Display prompt information
else
disp.text = "Try again"
end if
end sub
</script>
<form id="form1" runat="server"><br>
<asp:button id="disp" runat="server " onclick="disp" text="start"/>
<asp:image id="pic1" runat="server" width="50" height="50"/></form>
The running result of the program is shown in Figure 2 Shown:
Figure 2 Program running results
Conclusion
Pseudo-random numbers are used in many places in web applications. How to choose the seed parameters for generating random sequences, and which random algorithm to use in order to generate pseudo-random sequences with better performance are computer software developers. One of the goals pursued. Using the pseudo-random number generation class provided by the ASP.NET framework and the function methods provided by the scripting language VB.NET, various random sequences that meet different requirements can be generated. For example, the random check code used for identity authentication in the Web system uses random number generation technology. There are many articles on this website, so I will not go into details here.