« 用一条SQL得到Access数据库中的随机记录集. | Main | 无毒一身轻——21天改变体质 »

Randomize 语句

初始化随机数生成器。

Randomize [number]

number 参数可以是任何有效的数值表达式。

说明
Randomize 使用 number 参数初始化 Rnd 函数的随机数生成器,赋给它新的种子值。如果省略 number,则使用系统计时器返回的值作为新的种子值。

如果不使用 Randomize,则第一次调用 Rnd 函数(无参数)时,它将使用相同的数字作为种子值,随后使用最后生成的数值作为种子值。

注意 要重复随机数的序列,请在使用数值参数调用 Randomize 之前,立即用负值参数调用 Rnd。使用相同的 number 值的 Randomize 不能重复先前的随机数序列。

下面例子举例说明如何使用 Randomize 语句:

Dim MyValue, Response
Randomize '初始化随机数生成器。
Do Until Response = vbNo
MyValue = Int((6 * Rnd) + 1)' 产生 1 到 6 之间的随机数。
MsgBox MyValue
Response = MsgBox ("Roll again? ", vbYesNo)
Loop

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on May 26, 2006 10:20 AM.

The previous post in this blog was 用一条SQL得到Access数据库中的随机记录集..

The next post in this blog is 无毒一身轻——21天改变体质.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.