Latest web development tutorials

VBScript Rnd function

VBScript Reference Complete VBScript Reference

Rnd function returns a random number. Number is always less than 1 but greater than or equal to 0.

grammar

Rnd[(number)]

参数 描述
number 可选。有效的数值表达式。

如果数字是:

  • <0 - Rnd 会每次都返回相同的数字。
  • >0 - Rnd 会返回序列中的下一个随机数。
  • =0 - Rnd 会返回最近生成的数。
  • 省略 - Rnd 会返回序列中的下一个随机数。

Examples

Example 1

random number:

<script type="text/vbscript">

document.write(Rnd)

</script>

Please note that you will get the same number. To avoid this, use the Randomize statement in Example 2.

Examples of the above output:

0.7055475

try it"

Example 2

In order to avoid as in Example 1 to give the same numbers each time, use the Randomize statement:

<script type="text/vbscript">

Randomize
document.write(Rnd)

</script>

Examples of the above output:

0.4758112

try it"

Example 3

Here's how to produce random integers in a given range:

<script type="text/vbscript">

Dim max,min
max=100
min=1
Randomize
document.write(Int((max-min+1)*Rnd+min))

</script>

Examples of the above output:

71

try it"

VBScript Reference Complete VBScript Reference