在 VBA 中重复随机变量 [英] Repeating random variables in VBA

查看:37
本文介绍了在 VBA 中重复随机变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 randomize 和 rnd 来获取随机变量的重复列表?

How can I use randomize and rnd to get a repeating list of random variables?

通过重复列表,我的意思是如果您运行一个循环来获取 10 个随机数,则列表中的每个随机数都将是唯一的.另外,如果你再次运行这个序列,你会得到和以前一样的 10 个随机数.

By repeating list, I mean if you run a loop to get 10 random numbers, each random number in the list will be unique. In addition, if you were to run this sequence again, you would get the same 10 random numbers as before.

推荐答案

来自微软自己的口:

要重复随机数序列,请在使用带数字参数的 Randomize 之前立即用负参数调用 Rnd.

To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument.

有关详细信息,请参阅此处.

See here for details.

整个部分:

备注

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 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.

要生成给定范围内的随机整数,请使用以下公式:

To produce random integers in a given range, use this formula:

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

这里,upperbound 是范围内最大的数,lowerbound 是范围内最小的数.

Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.

注意 要重复随机数序列,请在使用带数字参数的 Randomize 之前立即调用带负参数的 Rnd.使用具有相同数字值的 Randomize 不会重复之前的序列.

Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.

举例来说,如果您将此代码放入 Excel,则每次运行它都会生成不同的数字:

By way of example, if you put this code into Excel, it generates a different number each time you run it:

Sub xx()
    ' x = Rnd(-1) '
    Randomize 10
    MsgBox (Rnd)
End Sub

但是,如果您取消注释 x = Rnd(-1) 行,它会在每次运行时生成 相同 编号.

However, if you uncomment the x = Rnd(-1) line, it generates the same number on each run.

请注意,您必须做两件事.使用负参数调用 Rnd 使用特定参数调用 Randomize.改变其中任何一个都会给你一个不同的种子(以及顺序).

Note that you have to do two things. Call Rnd with a negative argument and call Randomize with a specific argument. Changing either of those things will give you a different seed (and therefore sequence).

重新评论:

通过重复序列,我的意思是如果你运行一个循环来获得 10 个随机数,列表中的每个随机数都将是唯一的.此外,如果您再次运行此序列,您将获得与之前相同的 10 个随机数.我的描述有意义吗?

By repeating sequence, I mean if you run a loop to get 10 random numbers, each random number in the list will be unique. In addition, if you were to run this sequence again, you would get the same 10 random numbers as before. Does what I'm describing make sense?

您现在还需要一条信息.您要求的不是随机数,而是改组算法.我会向您推荐我之前给出的关于如何执行此操作的答案 此处.您需要做的就是将改组算法与上面详述的种子设置相结合,您将拥有可重复的独特序列.

You now need one more piece of information. What you're asking for is not random numbers but a shuffling algorithm. I'll refer you to an earlier answer I gave on how to do this here. All you need to do is combine the shuffling algorithm with the seed-setting detailed above and you'll have your repeatable, unique sequence.

这里有一些代码可以显示它的实际效果.这个子程序的每次运行都会返回序列 4 1 5 6 2 3 7 10 9 8 所以我认为这就是你所追求的,一个可重复的、随机"的、独特的序列.如果您希望能够生成不同的序列(但仍以可重复的方式),您只需要更改赋予 Randomize 的值.

And here's some code that shows it in action. Every run of this subroutine returns the sequence 4 1 5 6 2 3 7 10 9 8 so I think that's what you were after, a repetable, "random", unique sequence. If you wanted to be able to generate different sequences (but still in a repeatable manner), you only need to change the value given to Randomize.

Option Explicit
Option Base 1

Sub xx()
    Dim x(10) As Integer
    Dim xc As Integer
    Dim xp As Integer
    Dim i As Integer
    Dim s As String

    For i = 1 To 10
        x(i) = i
    Next
    xc = 10

    i = Rnd(-1)
    Randomize 1

    s = "Values:"
    For i = 1 To 10
        xp = Int(Rnd * xc) + 1
        s = s & " " & CStr(x(xp))
        x(xp) = x(xc)
        xc = xc - 1
    Next i

    MsgBox (s)
End Sub

这篇关于在 VBA 中重复随机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆