By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

random picture but not repeating

P: 5
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
Jul 1 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 5K+
P: 8,434
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.
Jul 1 '07 #2

P: 5
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?
Jul 1 '07 #3

Expert 5K+
P: 8,434
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).

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 RandomNumBetween function from this post).
Expand|Select|Wrap|Line Numbers
  1. Dim TheArray(1 to 6) As Long
  2. Dim I As Long, J As Long
  3. Dim FoundIt As Boolean
  4. Randomize
  5. For I = 1 To 6
  6.   Do
  7.     TempNum = RandomNumBetween(1, 6)
  8.     FoundIt = False
  9.     For J = 1 to (I - 1)
  10.       If TheArray(J) = TempNum Then
  11.         FoundIt = True
  12.         Exit For
  13.       End If
  14.     Next
  15.   Loop While FoundIt
  16.   TheArray(I) = TempNum
  17. Next
  18.  
Jul 1 '07 #4

P: 5
oh, okay. i got you.
thanks alot for your help. really appreciate it. =)
Jul 1 '07 #5

Expert 5K+
P: 8,434
Glad to help! :)

Let us know how it turns out.
Jul 1 '07 #6

P: 5
erm, it turn out fine, except that some of the times one picture would go missing, leaving a empty background.
Jul 1 '07 #7

P: 5
0 to 7 = a total of 8.
Jul 3 '07 #8

Expert 5K+
P: 8,434
0 to 7 = a total of 8.
I really hate zero-based arrays.
Jul 3 '07 #9

P: 5
yarr, my error was on the array part.
its now solve, thanks lots for ur help. =)
Jul 4 '07 #10

Expert 5K+
P: 8,434
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.
Jul 5 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.