how do I randomise images from an imagelist without repeats?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rizzsid11
    New Member
    • Nov 2011
    • 15

    how do I randomise images from an imagelist without repeats?

    I am trying to design a quiz where the user will be presented randomised images to grade. I am at the stage of writing code to randomise image without repeating. Images are stored in imagelist and are being randomised in picturebox. At the moment my code loops around continuously with repeats. However I would like it to present images randomly without repeating. Here's the randomise snippet of my code. Hellllppp!

    Code:
    ...Button Click
    Dim intPic As Integer
    Dim rand as New Random
    IntPic=rand.Next(0, Imagelist1.Images.Count
    PictureBox1.Image= Imagelist1.Images(intPic)
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You could remove the image from the list after you display it.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Not sure how useful this will be, since it looks as though you're using VB.Net. This is actually the area for the older pre-dotnet versions of VB, but we do try to help where possible. Just keep in mind you may find more relevant expertise over there.

      There's an article in the VB6 Insights area (about 3rd-last on the first page for me, may depend on your settings) showing how to produce non-repeating random numbers. This could form the basis of your routine, since those numbers can be used as your index. The code may need to be adjusted a little for your version of VB, but the logic should be valid.

      P.S. Note that Rabbit's idea of removing the images after using them both reduces memory usage over time, and keeps the code simple; both good things. So unless you need to retain the images for further use, that would definitely be the preferable technique.
      Last edited by Killer42; Dec 27 '11, 10:07 AM. Reason: Forgot the P.S.

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #4
        There is more than one way to solve the problem, all in function of how you stock the pictures.
        If you are using names instead of pictureboxes with indexes:
        - put the names in an array
        - randomize array index and check in the "UsedPic" array.
        - if not used: get the picture and put the name in an "UsedPic" array.
        - check if: ubound(usedpic) < number of pictures, else: " all pictures used"

        Comment

        • rizzsid11
          New Member
          • Nov 2011
          • 15

          #5
          Thanks all.

          Actually what I'm trying to build is ultimately is a image grading quiz. On my form there is a set of images numbered 1 to 5 (corresponding to a range of normal to severely normal reference images. In a picturebox random images will be presented every 60 seconds. Within the 60 seconds the user will be asked to grade the randomised image. The random image number and the user response needs to be saved in an excel file. The excel file will be sent back to me. I will ask users to repeat this exercise 4 weeks later to look for repeatability of the reference scales.


          @ Killer42 and Rabbit ...I did think about removing the images however if I need to record the random image number and user response then I guess it might not be the answer I'm looking for. ]
          @ Guido Geurs: I've put my images in the imagelist. At the moment I'm testing on only 8 images. So I'm looking at trying to handle 100 images. 1) I'm not sure if using imagelist is the direction I should pursue 2) how do I use a boolean array? I've tried to look at this but this keeps repeating images and wont't end the looping. :(

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            It sounds as though that VB6 non-repeating random number routine, or at least the logic it embodies, could be of help here. Shouldn't take much work to adjust for your VB version.

            Comment

            • Guido Geurs
              Recognized Expert Contributor
              • Oct 2009
              • 767

              #7
              If you want the results in Excel, is it not wise to use a form in VBA excel instead of VB6 ?

              Comment

              • Guido Geurs
                Recognized Expert Contributor
                • Oct 2009
                • 767

                #8
                This is a demo for loading pictures once and evaluate them.
                The filename is the index !
                The results are in an array.
                Attached Files

                Comment

                • Guido Geurs
                  Recognized Expert Contributor
                  • Oct 2009
                  • 767

                  #9
                  You can also use filenames instead of indexes: just fill the arrays with the filenames (like Apples.gif, houses.jpg, ...).
                  For setting the evaluation in the ARR_EVALUATION: run through the array : search for the filename in column 1 and set the evaluation in column 2.

                  Comment

                  • rizzsid11
                    New Member
                    • Nov 2011
                    • 15

                    #10
                    filenames...mig ht be too complicated...e ventually want this to be randomise upto 100 randomised images. So I've loaded the images in imagelist and labelled them as integers ranging from 0 upwards.

                    Comment

                    • rizzsid11
                      New Member
                      • Nov 2011
                      • 15

                      #11
                      @ Guido ...:( I can open the application but not any files....

                      Comment

                      • rizzsid11
                        New Member
                        • Nov 2011
                        • 15

                        #12
                        This is what i have so far...
                        'Random Class included in .NET
                        Private RND As New Random

                        'Create an array that can be used to hold the Index of Images to select from
                        Dim ImageIndexes As New List(Of Integer)

                        Private Sub Form6_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
                        'Populate the array with the Index of each Image in the ImageList
                        For i As Integer = 0 To ImageList1.Imag es.Count - 1
                        ImageIndexes.Ad d(i)
                        Next i
                        End Sub



                        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
                        '// To get a Random Image:
                        'Get a random Index within the array
                        Dim ArrayIndex As Integer = RND.Next(0, ImageIndexes.Co unt)

                        'Get the associated Image from the ImageList
                        PictureBox1.Ima ge = ImageList1.Imag es(ImageIndexes (ArrayIndex))

                        'Remove the reference to the current Image's Index from the ImageIndexes array (so it cannot be picked again)
                        'nb: We are not removing the Image, just a reference to it from within another array
                        ImageIndexes.Re move(ArrayIndex )
                        End Sub

                        Comment

                        • rizzsid11
                          New Member
                          • Nov 2011
                          • 15

                          #13
                          Code:
                              
                          'Random Class included in .NET
                              Private RND As New Random
                          
                              'Create an array that can be used to hold the Index of Images to select from
                              Dim ImageIndexes As New List(Of Integer)
                          
                              Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                                  'Populate the array with the Index of each Image in the ImageList
                                  For i As Integer = 0 To ImageList1.Images.Count - 1
                                      ImageIndexes.Add(i)
                                  Next i
                              End Sub
                          
                          
                          
                              Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                  '// To get a Random Image:
                                  'Get a random Index within the array
                                  Dim ArrayIndex As Integer = RND.Next(0, ImageIndexes.Count)
                          
                                  'Get the associated Image from the ImageList
                                  PictureBox1.Image = ImageList1.Images(ImageIndexes(ArrayIndex))
                          
                                  'Remove the reference to the current Image's Index from the ImageIndexes array (so it cannot be picked again)
                                  'nb: We are not removing the Image, just a reference to it from within another array
                                  ImageIndexes.Remove(ArrayIndex)
                              End Sub
                          Last edited by Niheel; Jan 6 '12, 07:09 PM.

                          Comment

                          • Guido Geurs
                            Recognized Expert Contributor
                            • Oct 2009
                            • 767

                            #14
                            This is the code for VB6 !!!:
                            form with:

                            ListBox List2
                            ListBox List1
                            CommandButton ComEvaluation (1)
                            CommandButton ComEvaluation (2)
                            CommandButton ComEvaluation (3)
                            CommandButton ComEvaluation (4)
                            CommandButton ComEvaluation (5)
                            Timer Timer1
                            TextBox TextNmbrPicture s
                            TextBox TextInterval
                            CommandButton ComStart
                            PictureBox PictureBox

                            Code:
                            Option Explicit
                            Dim ARR_SELECT() As Integer
                            Dim ARR_SELECTidx As Integer
                            Dim ARR_EVALUATION() As Integer
                            Dim ARR_EVALUATIONidx As Integer
                            Dim EVALUATIONvalue As Integer
                            Dim PICTUREnmbr As Integer
                            
                            Private Sub ComEvaluation_Click(Index As Integer)
                               EVALUATIONvalue = Index
                            End Sub
                            
                            Private Sub ComStart_Click()
                            Dim PICTUREScount As Integer
                            '§ ===BEGIN Just for development check================================
                                  List1.Clear
                                  List2.Clear
                            '§ ===END Just for development check================================
                               EVALUATIONvalue = 0
                               PICTUREScount = Val(TextNmbrPictures.Text)
                            '§ fill Array Evaluation
                               ReDim ARR_EVALUATION(1 To PICTUREScount, 1 To 2)
                               For ARR_EVALUATIONidx = 1 To PICTUREScount
                                  ARR_EVALUATION(ARR_EVALUATIONidx, 1) = ARR_EVALUATIONidx
                               Next
                            '§ fill Array Select
                               ReDim ARR_SELECT(0 To PICTUREScount)
                               For ARR_SELECTidx = 1 To PICTUREScount
                                  ARR_SELECT(ARR_SELECTidx) = ARR_SELECTidx
                               Next
                            '§ Start evaluation
                               Randomize ' Initialize random-number generator.
                               With Timer1
                                  .Interval = 1 '§ load immediately first picture
                                  .Enabled = True '§ start timer
                               End With
                            End Sub
                            
                            Private Sub Form_Load()
                            Me.ZOrder
                            End Sub
                            
                            Private Sub Timer1_Timer()
                               Timer1.Interval = Val(TextInterval.Text) * 1000
                               '§ save evaluation previous picture
                               If PICTUREnmbr <> 0 Then
                                  ARR_EVALUATION(PICTUREnmbr, 2) = EVALUATIONvalue
                            '§ ===BEGIN Just for development check================================
                                  List2.AddItem "Pic " & ARR_EVALUATION(PICTUREnmbr, 1) & " - Eval= " & ARR_EVALUATION(PICTUREnmbr, 2)
                            '§ ===END Just for development check================================
                               End If
                               EVALUATIONvalue = 0
                               If UBound(ARR_SELECT) > 0 Then
                                  '§ Generate random value between 1 and UBound(ARR_SELECT).
                                  ARR_SELECTidx = Int((UBound(ARR_SELECT) * Rnd) + 1)
                                  '§ load picture
                                  PICTUREnmbr = ARR_SELECT(ARR_SELECTidx)
                                  PictureBox.Picture = LoadPicture(App.Path & "\Pictures\" & PICTUREnmbr & ".GIF")
                                  '§ delete picture-number from ARR_SELECT
                                  ARR_SELECT(ARR_SELECTidx) = ARR_SELECT(UBound(ARR_SELECT))
                                  ReDim Preserve ARR_SELECT(0 To (UBound(ARR_SELECT) - 1))
                            '§ ===BEGIN Just for development check================================
                                  List1.AddItem PICTUREnmbr
                            '§ ===END Just for development check================================
                               Else
                                  Timer1.Enabled = False
                                  PictureBox.Picture = LoadPicture(App.Path & "\Pictures\END.GIF")
                               End If
                            End Sub
                            How it works:
                            "ARR_SELECT () As Integer" has the indexes for the filenames.
                            "ARR_EVALUATION () As Integer" has 2 columns:
                            1= file indexes
                            2= has the evaluations.
                            In the textboxes are the numbers for
                            TextBox TextNmbrPicture s = the number pictures to show
                            TextBox TextInterval = the durations between two pictures

                            the pictures are loaded from the HD with the index as name.

                            PS: List1 and List2 can be deleted= just for checking the program evolution!

                            Comment

                            • rizzsid11
                              New Member
                              • Nov 2011
                              • 15

                              #15
                              Hi Guido ...well after several days sick in bed I'm finally visiting your reply. Thank you for taking the time to reply to my query.

                              Hmm I'm using the Visual Studio 2008. So I'm getting lots of errors I've used the MSDN to identify a solution ...but am not getting very far. Hellllp...
                              Option Explicit default is On in VB 2008.
                              Not sure about how to set the evaluation index
                              List1.Clear().. .the Clear command is being underlined aswell as the .addItem
                              Redim has an error
                              ARR_EVALUATION is underlined - I'm unsure how to write this for my version of VB.
                              I'm getting a list of 18 errors :(. Am trying to work through them...

                              Comment

                              Working...