0

I am trying to create a loop where a randomly generated number is used to determine if a win/loss occurs up to a number of wins. The problem I am having is that when vba uses the RND function to define a variable, it is not generating a new random number after each loop. How can I make it so the win/loss is determined for each loop individually?

The code I am using is:

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
Jacob
  • 3
  • 1
  • you may want to read [this post](https://stackoverflow.com/questions/2884972/repeating-random-variables-in-vba). furthermore I can't grasp the logic of your code. for instance what is that `i=i` supposed to do? – DisplayName Mar 06 '18 at 08:49

2 Answers2

0

Swap the Rnd and Randomize lines

j = Rnd()
Do Until i = 26 Or z = 5000
Randomize

should be

Randomize
Do Until i = 26 Or z = 5000
j = Rnd()
Paul Kelly
  • 975
  • 7
  • 13
0

Maybe like this:

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