random picture but not repeating

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lifity
    New Member
    • Jul 2007
    • 5

    random picture but not repeating

    hi, i am stuck on part of my project and very new to VB.
    i had cut my picture into 6 equals part
    but need to display them randomly, without repeating the same part of the pic again, in random sequence.
    eg, pic1,pic2,pic3, pic4,pic5,pic6 to ==> pic3,pic2,pic6, pic1,pic5,pic4

    below are part of my code

    Dim theImage(0 To 7) As Object
    Dim myArray(0 To 7) As Object

    theImage(0) = pic1.Image
    theImage(1) = pic2.Image
    theImage(2) = pic3.Image
    theImage(3) = pic4.Image
    theImage(4) = pic5.Image
    theImage(5) = pic6.Image
    theImage(6) = pic7.Image

    For i = 0 To 6 Step 1
    Do
    random = num.Next(0, 7)
    Loop While (myArray(random ) <> 0)
    myArray(random) = theImage(i)

    Next i

    the part on the while loop wont work as they cant parse in image.
    how do i go about doing it? and my code seems sucky.
    thanks in advance
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    This topic has been covered once or twice, so you might find something by using the search box. But basically you need to track which numbers have been used, and not use them again. A couple of ways come to mind...
    • Create another array of "flags" (most likely boolean) to indicate which images have been used. Before allocating an image, check its corresponding flag. If it has been used, just loop around and generate another random number.
    • Instead of a second array of Object, just define myArray() as Long, and assign numbers to it instead of images. Then you can scan this target array to see whether the number is already in it before allocating each one, plus it might use less storage than an array Objects. To display the images, you would just use the entries in this array to index the theImage() array.

    Comment

    • lifity
      New Member
      • Jul 2007
      • 5

      #3
      oh, okay.
      but how do we assign the picture into the array if we use long/int?
      and while searching thru, a post said on assigning a unique number to the image, how do i do it?

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by lifity
        but how do we assign the picture into the array if we use long/int?
        The point was that you don't assign the pictures, you just assign the numbers of the pictures. In other words, the second array would just hold numbers 1-6, in a random sequence. To show the pictures, you just go through this array and use each number as an index for the first array (theImage).

        Originally posted by lifity
        and while searching thru, a post said on assigning a unique number to the image, how do i do it?
        Like I said, the general technique is to simply keep generating random numbers until you get one that you can't find in the array of used ones. For instance, here's a little sample routine to generate an array of six non-repeating numbers. It's probably not very elegant, as I'm just making it up on the spot. (Oh, also it calls the RandomNumBetwee n function from this post).
        [CODE=vb]
        Dim TheArray(1 to 6) As Long
        Dim I As Long, J As Long
        Dim FoundIt As Boolean
        Randomize
        For I = 1 To 6
        Do
        TempNum = RandomNumBetwee n(1, 6)
        FoundIt = False
        For J = 1 to (I - 1)
        If TheArray(J) = TempNum Then
        FoundIt = True
        Exit For
        End If
        Next
        Loop While FoundIt
        TheArray(I) = TempNum
        Next
        [/CODE]
        Last edited by Killer42; Jul 1 '07, 09:14 AM. Reason: Missed a couple of lines

        Comment

        • lifity
          New Member
          • Jul 2007
          • 5

          #5
          oh, okay. i got you.
          thanks alot for your help. really appreciate it. =)

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Glad to help! :)

            Let us know how it turns out.

            Comment

            • lifity
              New Member
              • Jul 2007
              • 5

              #7
              erm, it turn out fine, except that some of the times one picture would go missing, leaving a empty background.

              Comment

              • desperateinc
                New Member
                • Jul 2007
                • 5

                #8
                0 to 7 = a total of 8.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by desperateinc
                  0 to 7 = a total of 8.
                  I really hate zero-based arrays.

                  Comment

                  • lifity
                    New Member
                    • Jul 2007
                    • 5

                    #10
                    yarr, my error was on the array part.
                    its now solve, thanks lots for ur help. =)

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by lifity
                      yarr, my error was on the array part.
                      its now solve, thanks lots for ur help. =)
                      This is (partly) why I try to avoid zero-based arrays whenever possible. They are just too foreign to the way humans think. Great for computers, sure, but bad for humans.

                      Comment

                      Working...