# Rnd 函数Rnd function

## 语法Syntax

Rnd[(数字)]Rnd [ (Number) ]

## 备注Remarks

Rnd 函数返回一个小于 1 但大于或等于 0 的值。The Rnd function returns a value less than 1 but greater than or equal to zero.

_Number_的值决定了Rnd生成伪随机数字的方式:The value of Number determines how Rnd generates a pseudo-random number:

• 对于任何给定的原始种子，由于对 Rnd 函数的每个后续调用会将之前的数字用作序列中的下一个数字的种子，因此，将生成相同的数字序列。For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

• 在调用 Rnd 之前，请使用不带参数的 Randomize 语句来通过基于系统计时器的种子初始化随机数字生成器。Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.

``````Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

``````

[!注释] 若要重复随机数字的顺序，请先立即调用带负参数的 Rnd ，然后再将 Randomize 与数值参数一起使用。To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. 对_Number_使用具有相同值的随机化不会重复上一个序列。Using Randomize with the same value for Number does not repeat the previous sequence.

## 示例Example

``````Dim MyValue As Integer
MyValue = Int((6 * Rnd) + 1)    ' Generate random value between 1 and 6.

``````