Generation and Application of random numbers in ASP. NET

Source: Internet
Author: User
Tags random seed microsoft iis
【Abstract] Pseudo-Random numbers are widely used in computer software design. This article describes the general principles of pseudo-random number generation, and uses the random class provided in ASP. NET Framework and its methods to generate random numbers that meet various requirements in different ranges. Finally, the application of random numbers in ASP. Net in software design is described based on web control forms.

Keywords ASP. NET; pseudo-random number generation; web; random class

Random numbers are widely used in software design, especially in practice environment simulation and testing. In pursuit of real random sequences, people have used many primitive physical methods to generate a uniform distribution sequence that satisfies the precision (number of digits) within a certain range. Its disadvantages are: it is slow, inefficient, occupies a large amount of storage space, and cannot be reproduced. To meet the needs of Computer Simulation ResearchAlgorithmGenerates pseudo-random sequences that simulate various probability distributions. A pseudo-random number is a random number generated by a mathematical recursive formula. From a practical point of view, the simplest and most natural way to obtain such numbers is to use the random number generator provided by the function library of computer languages. Different Development Environments provide different functions and methods for generating random numbers. In typical cases, It outputs a pseudo-random variable value evenly distributed in the range of 0 and 1.

Random number generator

Use this formula to generate 0 ~ 65536 random numbers A1, A2 ,... SequentialProgramIt is called the multiplication harmonic and random number generator of the 232 step. Where B, C, and D are positive integers, and D is the seed of the random sequence generated by the formula.

From this formula, we can see that once the parameters B, C, and D are determined, the generated random sequence is also determined. This random number is called a pseudo-random number.

The following is an example of a random number generator. The random_seed function provides the random number seed to the user. When the form parameter D is 0, the current system time is used as the random number seed. When D is less than 0, D is used as the seed; the function random calculates a new seed based on a given seed and generates a range of low ~ The new random number of 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 );
}
}

ASP. NETPseudo-Random Number Generation Mechanism in

Computers cannot generate completely random numbers. The so-called random number generator uses a certain algorithm to perform complex operations on the pre-selected random seed, and uses the generated results to simulate a completely random number. This random number is called a pseudo-random number. A pseudo-random number is selected from a finite number with the same probability. The selected number is not completely random, but from a practical point of view, the random degree is enough.

1. VB. NET built-in functions

ASP. NET programs can use a varietyProgramming LanguageThe default programming language is VB. NET. Although VB. NET is similar to VB, their application environments are different.CodeMethods are also different. In VB. net, if you want to use mathematical functions, you must pass.. NET Framework provides the math class, which is located in the system namespace. You can add Imports system at the top of the Code. math allows you to use these mathematical functions.

The prototype of the function used to generate a random number is RND (x), which is used to generate a function between 0 and ~ A single-precision random number between 1. X is the seed that generates random numbers. If you want to randomly extract 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 with the random seed. To ensure that each pseudo-random number is "random", the selection of Random Seed is very important. If the selected random seed is the same, the generated random sequence is also the same. Generally, parameters related to system time are used as random seeds, which is also the default method used by the random number generator in. NET Framework.

2. Random Number System. Random

The random number class system. Random provides the following methods to generate random numbers that meet different requirements, as shown in table 1:

Table 1 methods provided by the system. Random class

NO. method name function description
1 next () Returns a value ranging from 0 ~ Integer between 2147483647
2 next (I) Returns a value ranging from 0 ~ Integer between I
3 next (I, j) Returns An I ~ Integer between j
4 nextdouble () Returns a value ranging from 0 ~ Random decimal number between 1
5 nextdouble (byte () use 0 ~ A random integer between 255 is used as the value of each element in the byte array.

The random number class system. Random must be declared first. If you want to use the nextbytes (byte () method, you must declare a byte array before use.

3. Compile the test program

Before running the ASP. NET program, you must establish a development and running environment, including configuring Microsoft IIS and installing the. NET Framework SDK and the MSE editor. The MSE editor is an additional software for Microsoft Office and is very convenient as an ASP. NET editing tool. The test 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>

Instance Development

Application Requirement Analysis: Create an application to simulate a dice game. In this game, players will randomly scroll a dice. Each dice has six sides, representing the six points 1, 2, 3, 4, 5, and 6 respectively. When the dice stops, observe the points on the dice. If the number of points is 6, the system prompts the winning information. Otherwise, the system continues to throw.

1. Key Technologies

1.1 web control forms

 Web control forms have the object-oriented feature of advanced languages. They are the objects of the system. Web. UI. webcontrols namespace in ASP. NET. The processing process is similar to the HTML control form, which has the following features: the form runs on the server side; the input field is a control, which has powerful and rich attributes and methods, the information of the input field can be retained, the ability to verify the input field, and the ability to include data display controls.

A typical web control form code is as follows:

<Asp: button id = "sub3" text = "Confirm" runat = "server"/>

ASP: XXX indicates the type of Web Control, Id indicates the Control ID, and runat indicates the control that runs on the server.

The Web Control used in this example is mainly a button control. The image control is used to display image files. It has the following attributes: imageurl, indicating the URL of the image file, width, width of the image file display, height, and height of the image file display.

1.2 pseudo-random number generation

According to the requirement analysis description, to simulate the number of points obtained by throwing a dice at random, the random generation range is 1 ~ An integer between 6. At this time, the next (I, j) method of the system. Random class can be used. Here I = 1, j = 7. (I wrote a test program and found that if J = 6, the random number generated is 1 ~ Between 5)

2. Program Implementation

The main code of this program is as follows:

<Script language = "VB" runat = "server">

Sub disp (OBJ as object, e as eventargs)

Dim R1 as random = new random () // defines a random number class

Dim file_prefix as string = "Images \ die"

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 the 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>

Conclusion

Pseudo-Random numbers are used in many places in Web applications. How to select the seed parameter for generating random sequences, and which random algorithms are used to generate pseudo-random sequences with better performance is one of the goals pursued by computer software developers. The ASP. NET Framework provides a pseudo-random number generation class and functional methods provided by the script language VB. NET to generate various random sequences meeting different requirements. For example, the random verification code used for identity authentication in the web system uses the random number generation technology, which has manyArticleIntroduction. I will not go into details here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.