Results 1 to 8 of 8

Thread: [RESOLVED] how to remove a repeating number rnd

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Resolved [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.

  2. #2

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: how to remove a repeating number rnd

    Try this
    vb Code:
    1. Dim intCreatedNum(9) As Integer
    2.     Dim blnExist As Boolean
    3.    
    4.     For X = 1 To 9
    5.         Num = (X - 1) \ 4 + 1 ' make numbers 1,1,1,1,2,2,2,2,3
    6.         blnExist = False
    7.         Do
    8.             N = Int(Rnd * 9) + 1 ' random integer 1 to 9
    9.             For j = 1 To 9
    10.                 If N = intCreatedNum Then
    11.                     blnExist = True
    12.                     Exit For
    13.                 End If
    14.             Next
    15.         Loop Until blnExist = False
    16.        
    17.         intCreatedNum(j) = N
    18.         cmdTile(N).Caption = Num
    19.     Next X



  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: how to remove a repeating number rnd

    If N = intCreatedNum Then
    It says type mismatch.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: how to remove a repeating number rnd

    Quote Originally Posted by public View Post
    If N = intCreatedNum Then
    It says type mismatch.
    Yes, it should be If N = intCreatedNum(j) Then



  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: how to remove a repeating number rnd

    intCreatedNum(j) = N
    subscript out of range

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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...



  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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
  •  



Click Here to Expand Forum to Full Width