Private Sub Command1_Click()
Dim x As Integer
For x = 1 To 100
List1.AddItem (Int(100 * Rnd()))
If ((Int(100 * Rnd())) >= 10) Then
Print
Else
End If
Next x
End Sub
views:
123answers:
4I think you may have mistyped this code, as I don't see what you are printing. Also, in general you should probably assign your random number to a temp variable. As is, the random number you are adding to the list is not the same as the one in your if block.
Looks like it's because you're generating a random number once, and adding it to List1; then you generate another, different random number and conditionally print it.
You are not doing any filtering on what numbers get added to List1, and I'm not quite sure what you're printing (I'm not a VB guy).
The error seems to be in two parts. First, you are adding a number to your list that is different then the one you are comparing.
The second is that you are using Rnd in the wrong manner. See http://msdn.microsoft.com/en-us/library/f7s023d2%28VS.80%29.aspx for usage but basically, the way your code is set you are generating a number between 0 and 100. Because rnd returns a single.
What's happening is that your basically doing 100 * 0.5 which returns you 50 or 100 * 0.01 which returns you 1.
If you want a lower bound of 100 you have to do it like this.
CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
Also, please don't forget to call Randomize() before using Rnd
If you want the random numbers to range from 10 to 99, you need to calculate them a bit differently. There are 90 different possible values, so that is what you multiply Rnd() by. Then you add the minimum value, which is 10:
Private Sub Command1_Click()
Dim x As Integer
For x = 1 To 100
List1.AddItem(Int(90 * Rnd()) + 10)
Next x
End Sub