我正在尝试创建一个循环,其中使用随机生成的数字来确定是否赢得了多次胜利。我遇到的问题是,当vba使用RND函数定义变量时,它不会在每个循环后生成一个新的随机数。我怎样才能使每个循环单独确定赢/输?
我使用的代码是:
Sub wins()
Dim i As Integer
Dim j As Integer
Dim z As Variant
j = Rnd()
Do Until i = 26 Or z = 5000
Randomize
If j > 0.55 Then i = i + 1
If j < 0.55 And i = 0 Then i = i
If j < 0.55 And i > 0 Then i = i + 1
z = z + 1
Loop
msgbox z
End Sub
答案 0 :(得分:0)
交换Rnd和Randomize行
j = Rnd()
Do Until i = 26 Or z = 5000
Randomize
应该是
Randomize
Do Until i = 26 Or z = 5000
j = Rnd()
答案 1 :(得分:0)
也许是这样的:
Sub wins()
Dim i As Integer
Dim j As Integer
Dim z As Variant
Do Until i = 26 Or z = 5000
Randomize
j = Rnd()
If j > 0.55 Then
i = i + 1
If j < 0.55 And i = 0 Then
i = i
If j < 0.55 And i > 0 Then
i = i + 1
z = z + 1
Loop
MsgBox z
End Sub