Today I am trying to run basic loop using "for". I would like to run a loop till I get 20 random numbers between 26 - 28 in MsgBox.
Sub C7()
Dim b As Integer
Dim f As Single
Dim ret As String
For b = 1 To 20
f = Rnd() * 28
If f >= 26 And f <= 28 Then
ret = ret & Str(f)
End If
Next b
MsgBox ret
End Sub
Following code runs the loop 20-times and gives me all values between 26 - 28 (usually not more than 3). What I need is a code which will run the loop X-times until it gives me 20 numbers between 26 - 27.
Every advice is welcome! Thanks
Change the randomize to give a number less than 3, then add 26.
Rnd() returns a number between [0, 1), multiplying that by 28 will never result in 28. And frequently will result in a number below 28, which gets avoided in the output because of your if block.
f = (Rnd() * 3)
will give a random number between [0, 3).
f = ((Rnd() * 3) + 26)
will give a random number between [26, 29).