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

查看:220
本文介绍了在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.

推荐答案

从Microsoft自己的嘴中了解:

From Microsoft's own mouth:

要重复随机数序列,请在使用带有数字参数的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但大于或等于零.

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)

在这里,上限是范围内的最高数字,而下限是范围内的最低数字.

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天全站免登陆