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
1
  • you may want to read this post. furthermore I can't grasp the logic of your code. for instance what is that i=i supposed to do? Commented Mar 6, 2018 at 8:49

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.