RND() is a very important function in ASP. If you want to create a random greeting, a random prompt for dates, etc., you will use this function, then do you know how to use it? Now let’s talk to the editor to explain in detail the use of ASP random number RND().
ASP function rnd()
The function rnd() returns a random number between 0 and 1.
How to use it is as follows:
The code copy is as follows:response.write rnd() 'Its possible return value: 0.2357746
If you want to use the rnd() function to return integers in a certain range, for example, numbers that are greater than or equal to 0 and less than or equal to a specific integer upperbound, you can use the following method:
The code copy is as follows:response.write int((upperbound+1)*rnd)
For example, the following statement will return an integer between 0 and 5, including 0 and 5:
The code copy is as follows:response.write int((5+1)*rnd)
If you want to get a random number in a range that has a lower bound greater than 0, you can use the following method:
The code copy is 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:
The code copy is as follows:response.write int((75-50+1)*rnd+50)
However, there is a problem that a random number is generated, but it is the same random number every time. This may puzzle you. There is a special statement that can help solve this 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.
The function RND() is a very important function. If you want to create a random greeting, a random prompt for a date, or even a game, you will want to use this function.
The function RND() returns a random number between 0 and 1. Here is an example of this function and its possible return values:
<%=RND()%>
0.7055643
Typically, you are more interested in using this function to return integers within a certain range. To return a number greater than or equal to 0 and less than a specific integer, you can use the following statement:
<%=INT((upperbound+1)*RND)%>
Replace the expression upperbound with the maximum random number you want to generate. For example, the following script returns a number between 0 and 5, including 0 and 5:
<%=INT(5+1)*RND)%>
If you want to generate a random number within a range that has a lower bound greater 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 the function RND(), it will return the same random numbers in the same order, which may surprise you. Consider the following example:
The code copy is 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 see you!”
END SELECT
%>
<%=Greeting%>
This script creates and prints a random greeting. However, it may not work the way you think it is. Whenever someone downloads a webpage containing this script, the same random greeting will be printed. If someone returns to this page multiple times, he or she will get the same greetings. A random number is generated, but it is the same random number every time.
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 above example to work correctly:
The code copy is 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 see you!”
END SELECT
%>
<%=Greeting%>
This script works correctly. Each time this script is executed, a new random greeting will be 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 numeric values generated by the function RND(), you can use the following script to determine it:
The code copy is 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 generates 100 random numbers between 0 and 9. It tracks how many random numbers are generated for each value. Finally, it prints a bar chart representing the results.
The above is the editor’s detailed explanation of the use of ASP random number RND(). I hope it will be helpful to you. If you want to view more related content, please follow the WoXin Technology Channel. Thank you for your support!