|
-
Apr 9th, 2012, 03:41 PM
#1
Thread Starter
Lively Member
[RESOLVED] how to remove a repeating number rnd
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.
-
Apr 9th, 2012, 04:21 PM
#2
Re: how to remove a repeating number rnd
This is one of most covered topics - simply search forum for many solutions.
-
Apr 9th, 2012, 04:21 PM
#3
Re: how to remove a repeating number rnd
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
-
Apr 9th, 2012, 04:41 PM
#4
Thread Starter
Lively Member
Re: how to remove a repeating number rnd
If N = intCreatedNum Then
It says type mismatch.
-
Apr 9th, 2012, 04:51 PM
#5
Re: how to remove a repeating number rnd
 Originally Posted by public
If N = intCreatedNum Then
It says type mismatch.
Yes, it should be If N = intCreatedNum(j) Then
-
Apr 9th, 2012, 05:48 PM
#6
Thread Starter
Lively Member
Re: how to remove a repeating number rnd
intCreatedNum(j) = N
subscript out of range
-
Apr 9th, 2012, 06:12 PM
#7
Re: how to remove a repeating number rnd
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...
-
Apr 10th, 2012, 01:39 AM
#8
Re: how to remove a repeating number rnd
There's a logic error:
Code:
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
When the Do loop exits, j will always be 10
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)
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
and you need to define all your variables.
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|