For X = 1 To 9
Num = (X - 1) \ 4 + 1 ' make numbers 1,1,1,1,2,2,2,2,3
N = Int(Rnd * 9) + 1 ' random integer 1 to 9
cmdTile(N).Caption = Num
Next X
N repeats so some tiles are blank help with code.
Printable View
For X = 1 To 9
Num = (X - 1) \ 4 + 1 ' make numbers 1,1,1,1,2,2,2,2,3
N = Int(Rnd * 9) + 1 ' random integer 1 to 9
cmdTile(N).Caption = Num
Next X
N repeats so some tiles are blank help with code.
This is one of most covered topics - simply search forum for many solutions.
Try this
vb Code:
Dim intCreatedNum(9) As Integer Dim blnExist As Boolean For X = 1 To 9 Num = (X - 1) \ 4 + 1 ' make numbers 1,1,1,1,2,2,2,2,3 blnExist = False Do N = Int(Rnd * 9) + 1 ' random integer 1 to 9 For j = 1 To 9 If N = intCreatedNum Then blnExist = True Exit For End If Next Loop Until blnExist = False intCreatedNum(j) = N cmdTile(N).Caption = Num Next X
If N = intCreatedNum Then
It says type mismatch.
intCreatedNum(j) = N
subscript out of range
How did you declared intCreatedNum?
Change this line For j = 1 To 9 with according to your declaration. e.g. if you declared intCreatedNum(7) then replace 9 with 7, etc...
There's a logic error:
When the Do loop exits, j will always be 10Code:For j = 1 To 9
If N = intCreatedNum(j) Then
blnExist = True
Exit For
End If
Next
Loop Until blnExist = False
intCreatedNum(j) = N
cmdTile(N).Caption = Num
You'll need to introduce another variable to assign the value to the array. I'd also define the array as having 9 elements rather than 10 and start the loop at 0 rather than one (and end it with 8)
and you need to define all your variables.Code:For j = 0 To 8
If N = intCreatedNum Then
blnExist = True
Exit For
End If
Next
Loop Until blnExist = False
intCreatedNum(k) = N
k = k + 1
cmdTile(N).Caption = Num
I'd take Rhino's advice and do a search on the forums using something like "Shuffle numbers" or "non repeating random numbers" as a search term