Function RND() is a very important function. If you want to build a random greeting, a random prompt for a date, or even a game, you will want to use this function
ASP function rnd()
The function rnd() returns a random number between 0 and 1.
How to use it:
Copy the code code as follows:
response.write rnd() 'Possible return value: 0.2357746
If you want to use the rnd() function to return integers within a certain range, for example, numbers greater than or equal to 0 and less than or equal to a specific integer upperbound, you can use the following method:
Copy the code code as follows:
response.write int((upperbound+1)*rnd)
For example, the following statement will return an integer between 0 and 5, inclusive:
Copy the code code as follows:
response.write int((5+1)*rnd)
If you want to get a random number within a certain range that has a lower bound greater than 0, you can use the following method:
Copy the code code as follows:
response.write int((upperbound-lowerbound+1)*rnd+lowerbound)
For example, the following script produces an integer between 50 and 75, inclusive:
Copy the code code as follows:
response.write int((75-50+1)*rnd+50)
However, there is a problem. A random number is generated, but it is the same random number every time. This may make you very confused. There is a special statement that can help solve this problem, and that is the randomize statement.
The randomize statement is used to force the function rnd to use a new random number sequence. The randomize statement provides a new seed value to the function rnd through the computer's system timer.
Function RND() is a very important function. If you want to build a random greeting, a random prompt for a date, or even a game, you'll 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 an integer within a certain range. To return a number that is greater than or equal to 0 but less than a specific integer, you can use the following statement:
<%=INT((upperbound+1)*RND)%>
Replace the expression upperbound with the largest random number you want to generate. For example, the following script returns a number between 0 and 5, inclusive:
<%=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 generates a random number between 50 and 75 (inclusive):
<%=INT((75-50+1)*RND+50)%>
It may surprise you that whenever you use the function RND(), it returns the same random numbers in the same order. Consider the following example:
Copy the 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 see you!”
END SELECT
%>
<%=Greeting%>
This script creates and prints a random greeting. However, it's possible that it doesn't work the way you imagine. Whenever someone downloads a web page 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 greeting. A random number is generated, but it's the same random number every time.
There is a special statement that helps with this problem. The RANDOMIZE statement is used to force the function RND() to use a new random number sequence. The RANDOMIZE statement provides a new seed value to function RND() through the computer's system timer. The following example shows how to modify the above example so that it works correctly:
Copy the 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 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 values produced by the function RND(), you can determine it with the following script:
Copy the 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 generates 100 random numbers between 0 and 9. It keeps track of how many random numbers are generated for each value. Finally, it prints a bar chart representing the results.