ASP random number rnd () use method _ Application technique

Source: Internet
Author: User

ASP function rnd ()

function rnd () returns a random number from 0 to 1.

Use the following methods:

Copy Code code as follows:

Response.Write Rnd () ' Its possible return value: 0.2357746

If you want to use the Rnd () function to return a range of integers, such as a number greater than or equal to 0 and less than equal to a particular integer upperbound, you can use the following method:

Copy Code code as follows:

Response.Write Int ((upperbound+1) *rnd)

For example, the following statement returns an integer between 0 and 5, including 0 and 5:

Copy Code code as follows:

Response.Write Int ((5+1) *rnd)

If you want to get a random number in a range that has a lower bound lowerbound greater than 0, you can use the following method:

Copy Code code as follows:

Response.Write Int ((upperbound-lowerbound+1) *rnd+lowerbound)

For example, the following script produces an integer between 50 and 75, including 50 and 75:

Copy Code code as follows:

Response.Write Int ((75-50+1) *rnd+50)

However, there is a problem, a random number is produced, but each time it is the same random number. This may make you very puzzled, there is a special statement to help solve the problem, that is the Randomize statement.

The Randomize statement is used to force the function Rnd to use a new sequence of random numbers. The Randomize statement provides a new seed value for the function Rnd through the computer's system timer.

function Rnd () is a very important function. If you want to create a random greeting, a random hint of a date, or even a game, you're going to use this function.

function rnd () returns a random number from 0 to 1. Here is an example of this function and its possible return value:
<%=rnd ()%>
0.7055643
Typically, you are more interested in using this function to return an integer that is in a certain range. To return a number that is greater than or equal to 0 and is less than a specific integer, you can use the following statement:
<%=int ((upperbound+1) *rnd)%>
Replace the expression upperbound with the maximum number of random numbers you want to produce. For example, the following script returns a number between 0 and 5, including 0 and 5:
<%=int (5+1) *rnd)%>
If you want to produce a random number in a range that has a lower bound than 0, you can use the following script:
<%=int ((Upperbound–lowerbound + 1) *rnd+lowerbound)%>
For example, the following script produces a random number between 50 and 75 (including 50 and 75):
<%=int ((75-50+1) *rnd+50)%>
Whenever you use a function rnd (), it will return the same random number in the same order, which may surprise you. Consider the following example:

Copy Code code as follows:

<%
Pick_greeting=int ((2+1) *rnd)
SELECT Case Pick_greeting
Case 0
Greeting= "welcome!"
Case 1
Greeting= "Hello!"
Case 2
Greeting= "Happy to the you!"
End SELECT
%>
<%=Greeting%>

This script creates and prints a random greeting. However, it may not work the way you think. Whenever someone downloads a page containing this script, the same random greeting is printed. If someone returns to this page many times, he or she will get the same greeting. A random number is produced, but each time it is the same random number.
There is a special statement that can help solve this problem. The Randomize statement is used to force the function rnd () to use a new sequence of random numbers. The Randomize statement provides a new seed value for the function rnd () through the computer's system timer. The following example shows how to modify the example above to make it work correctly:

Copy Code code as follows:

<%
RANDOMIZE
Pick_greeting=int ((2+1) *rnd)
SELECT Case Pick_greeting
Case 0
Greeting= "welcome!"
Case 1
Greeting= "Hello!"
Case 2
Greeting= "Happy to the you!"
End SELECT
%>
<%=Greeting%>

This script will work correctly. Each time the script is executed, a new random greeting is generated. The Randomize statement forces the function rnd () to use a new sequence of random numbers.
Finally, if you are curious about the distribution of the values produced by the function Rnd (), you can use the following script to determine it:

Copy Code code as follows:

<%
CONST upperbound=9,iterations=100
REDIM DIST (Upperbound)
RANDOMIZE
For I=1 to iterations
Rnd_num=int ((upperbound+1) *rnd)
DIST (Rnd_num) =dist (rnd_num) & "#"
NEXT
For I=0 to Upperbound
%>
<%=i& ":" &dist (i)%><br>
<%
NEXT
%>

This script produces 100 random numbers from 0 to 9. It tracks how many random numbers are generated for each value. Finally, it prints a bar chart that represents the result.

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.