VBScript » Functions » Rnd

Syntax:
Rnd(Number)

The Rnd function generates a pseudo-random number greater than or equal to 0.0 and less than 1.0.

The Number argument is optional, but different numbers will give different pseudo-random numbers.

Note that the number generated is pseudo-random, because the same output tends to be repeated over and over. You can use the Randomize statement to over come this problem.

Examples

Code:
<% =Rnd() %>
Output:
0.7055475
Language(s): VBScript
Code:
<% =Rnd(127.89) %>
Output:
0.533424
Language(s): VBScript
Code:
<% =Rnd(-127.89) %>
Output:
0.6953956
Language(s): VBScript
Code:
<% Randomize %>
<% =Rnd() %>
Output:
0.1414095
Language(s): VBScript
Code:
<% upperlimit = 50000.0 %>
<% lowerlimit = -30000.0 %>
<% =Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit)%>
Output:
30366
Explanation:

This code will generate a random number between any limits you choose. (Since the Int function always rounds down, we add one to the difference between the limits.)

Language(s): VBScript

See Also: