VBScript RndFunction
Complete VBScript Reference
The Rnd function returns a random number. The number is always less than 1 but greater than or equal to 0.
Syntax
Rnd[(number)]
| Parameter | Description |
|---|---|
| number | Optional. A valid numeric expression. If the number is:
|
Examples
Example 1
Random number:
<script type="text/vbscript">
document.write(Rnd)
</script>
document.write(Rnd)
</script>
Note that you will get the same number every time. To avoid this, use the Randomize statement as in Example 2.
Output of the above example:
0.7055475
Try it Yourself »
Example 2
To avoid getting the same number every time as in Example 1, use the Randomize statement:
<script type="text/vbscript">
Randomize
document.write(Rnd)
</script>
Randomize
document.write(Rnd)
</script>
Output of the above example:
0.4758112
Try it Yourself »
Example 3
The following shows how to produce random integers within a given range:
<script type="text/vbscript">
Dim max,min
max=100
min=1
Randomize
document.write(Int((max-min+1)*Rnd+min))
</script>
Dim max,min
max=100
min=1
Randomize
document.write(Int((max-min+1)*Rnd+min))
</script>
Output of the above example:
71
Try it Yourself »
Complete VBScript Reference Other Extensions