在 VBA 中重复随机变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2884972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:27:39  来源:igfitidea点击:

Repeating random variables in VBA

vbarandom

提问by sooprise

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

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

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.

通过重复列表,我的意思是如果您运行一个循环来获取 10 个随机数,则列表中的每个随机数都将是唯一的。此外,如果您再次运行此序列,您将获得与之前相同的 10 个随机数。

回答by paxdiablo

From Microsoft's own mouth:

来自微软自己的口中:

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

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

See herefor details.

有关详细信息,请参见此处

The whole section:

全段:



Remarks

评论

The Rndfunction returns a value less than 1 but greater than or equal to zero.

Rnd函数返回一个小于 1 但大于或等于 0 的值。

The value of number determines how Rndgenerates a random number:

number 的值决定了如何Rnd生成随机数:

For any given initial seed, the same number sequence is generated because each successive call to the Rndfunction uses the previous number as a seed for the next number in the sequence.

对于任何给定的初始种子,都会生成相同的数字序列,因为对该Rnd函数的每次连续调用都使用前一个数字作为序列中下一个数字的种子。

Before calling Rnd, use the Randomizestatement without an argument to initialize the random-number generator with a seed based on the system timer.

在调用 之前Rnd,使用Randomize不带参数的语句以基于系统计时器的种子初始化随机数生成器。

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

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

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

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

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

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

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



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

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

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

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

但是,如果您取消注释该x = Rnd(-1)行,它会在每次运行时生成相同的数字。

Note that you have to do twothings. Call Rndwith a negative argument andcall Randomizewith a specific argument. Changing either of those things will give you a different seed (and therefore sequence).

请注意,您必须做件事。调用Rnd带负参数,并调用Randomize具有特定参数。改变其中任何一个都会给你一个不同的种子(因此顺序)。



Edit:

编辑:

Re your comment:

回复您的评论:

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?

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

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.

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



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

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

回答by omegastripes

Actually there is no need to use Randomizeto repeat Rndpseudo-random sequence.

实际上没有必要使用Randomize重复Rnd伪随机序列。

Take a look at the below line of code. It simply outputs 10 random numbers:

看看下面的代码行。它只是输出 10 个随机数:

MsgBox Join(Array(Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd()), vbCrLf)

If you run it first time (i. e. with initial seed) or after resetting VBA code execution (reinitializing that way), then the output will be as follows:

如果您第一次运行它(即使用初始种子)或在重置 VBA 代码执行后(以这种方式重新初始化),那么输出将如下所示:

0,7055475
0,533424
0,5795186
0,2895625
0,301948
0,7747401
1,401764E-02
0,7607236
0,81449
0,7090379

That sequence is returned in any VBA or VBS code for initial seed.

该序列在初始种子的任何 VBA 或 VBS 代码中返回。

To repeat that sequence you just need to specify the seed by passing a negative number to Rndfunction as argument:

要重复该序列,您只需要通过传递一个负数Rnd作为参数来指定种子:

MsgBox Join(Array(Rnd(-8716085), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd()), vbCrLf)

Or even so:

甚至是这样:

Rnd -16184694
MsgBox Join(Array(Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd(), Rnd()), vbCrLf)

The same with any other sequence, you need specify the seed as Rndargument only.

与任何其他序列相同,您只需将种子指定为Rnd参数。