【发布时间】:2018-03-22 15:05:40
【问题描述】:
我正在尝试在 VBA 中复制线性同余生成器,但我的程序返回错误“6”:溢出...
Sub test()
Dim a As Long, c As Long, period As Long
Dim seed As Long, sample As Long, max As Long
Dim i As Long
seed = 1234
sample = 2
max = 100
a = 48271
c = 0
period = 2 ^ 31 - 1
For i = 1 To sample
seed = (a * seed + c) Mod period
Next i
End Sub
我认为问题在于for循环的第一个表达式,详细
a*seed
在循环的第二步。 任何解决问题而不分裂的建议
a*seed
在
(100*seed+100*seed+100*seed+...+(a-100*n)*seed
【问题讨论】:
-
@CallumDA 我尝试将每个数字声明为 Long 但它仍然会溢出
-
调试函数说seed = (a*seed+c) Mod period
-
我已经更新了你的问题,所以代码是最小的并且可以立即重复——因为我已经更新了声明并且问题仍然存在我现在也删除了我的答案
-
好的,非常感谢!
-
@MathieuGuindon,是的。我将
seed =换成了Debug.Print,它工作得很好——输出是59566414。在Long的范围内