我在VBA中进行了蒙特卡洛模拟。客户想要(不要问为什么)修复随机数序列,即每次运行模型时,序列都应保持不变。我按照here所述修复了随机种子。但是在不同的PC上它是不同的。知道为什么以及如何在不同的计算机上修复它吗?

最佳答案

您可以将rnd函数与否定参数一起使用以获取重复的随机数列表。

这是文档的链接:

http://office.microsoft.com/en-us/access-help/rnd-function-HA001228901.aspx

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.



Sub TestRandomNumberSequence()

rnd (-10)

    For i = 1 To 5

        Randomize 10
        MsgBox BetweenRange(1, 20, rnd)

    Next i

    'always returns the following sequence

    '5
    '18
    '19
    '6
    '17

End Sub

Function BetweenRange(min As Integer, max As Integer, ByVal rnd As Double) As Integer

BetweenRange = Int((max - min + 1) * rnd + min)

End Function

关于excel - 在VBA中重复随机数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22908152/

10-10 16:19