VBScript Rnd Function
Definition and Usage
The Rnd function can return a random number. The number is always less than 1 but greater than or equal to 0.
Since the Rnd function uses the previous number in the sequence as the seed for the next number each time it is called sequentially, the same sequence of numbers will be generated for any initially given seed.
Before calling Rnd, use the Randomize statement without parameters to initialize the random number generator, which has a seed based on the system timer.
To generate a random integer within a specified range, please use the following formula:
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Here, upperbound is the upper limit of this range, and lowerbound is the lower limit within this range.
Note:To repeat the random number sequence, call Rnd with a negative value parameter immediately before using the numeric parameter to call Randomize. Using the same number for Randomize cannot repeat the previous random number sequence.
Syntax
Rnd[(number)]
Parameter | Description |
---|---|
number |
Optional. A valid numeric expression. If the number is:
|
Example
Example 1
document.write(Rnd)
Output:
0.7055475
Example 2
If you use the code in Example 1, the same random number will appear repeatedly.
You can use the Randomize statement to generate a new random number each time the page is reloaded:
Randomize document.write(Rnd)
Output:
0.4758112
Example 3
dim max,min max=100 min=1 document.write(Int((max-min+1)*Rnd+min))
Output:
71