Problem Randomise images no repeats

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 6795 Views - Last Post: 08 February 2012 - 04:18 PM Rate Topic: -----

#1 Ailaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 10-December 11

Problem Randomise images no repeats

Posted 07 January 2012 - 04:11 AM

I am a complete newbie at vb 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 (not sure if this is the best way to 100 images - I've read a few posts about limitations of imagelist) and are being randomly presented 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! it loops round and I'm not sure about to remove images.

I've tried to look at Boolean arrays...but I don't seem to be able to do this ....

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. Ultimately I'd like the random images to be working through the timer control....trying to so this one step at a time so I understand it.

Button Click 
Dim intPic As Integer 
Dim rand as New Random 
IntPic=rand.Next(0, Imagelist1.Images.Count 
PictureBox1.Image= Imagelist1.Images(intPic) 


Is This A Good Question/Topic? 0
  • +

Replies To: Problem Randomise images no repeats

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5503
  • View blog
  • Posts: 11,808
  • Joined: 02-June 10

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 06:36 AM

I think the most direct logic is to remove the randomly selected elements.

So you start with a List<Image> that has 100 elements.
Randomly select elements 1, 14, 6, and 87
Remove them from the array.

Now when you randomly select they simply aren't there to choose from.
Was This Post Helpful? 1
  • +
  • -

#3 Ailaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 10-December 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 03:00 PM

Eeeeekk...not sure if I know what you mean...ok I'll look up Lists....
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5503
  • View blog
  • Posts: 11,808
  • Joined: 02-June 10

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 03:05 PM

A List<Image> is just an advanced version of Image[].
One of the benefits for you is there is a method for .RemoveAt(int) where you can specify the element to remove by index. Perfect for your situation where you want to remove at random indexes.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 03:07 PM

What is he saying is as follows:
- Put your images in a list
- Randomly generate a number between 0 to the size of the list
- Use the number generated as an index to the list to get that image
- After using image, remove the image at that index
- Again generate another number and follow as above

What you need to remember is that in every loop the size of the list is decreasing, so your random generator must consider that.
Look at post #6 in the tutorial below and you will see an example:
http://www.dreaminco...random-numbers/

This post has been edited by smohd: 07 January 2012 - 03:33 PM

Was This Post Helpful? 2
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5503
  • View blog
  • Posts: 11,808
  • Joined: 02-June 10

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 03:52 PM

What's nice is the OP is already taking that into consideration. Nice to see someone not hardcoding values.


4 IntPic=rand.Next(0, Imagelist1.Images.Count

But to be honest, it worries me when such a simple bit of logic has to be repeated in increasingly more verbose detail until the OP 'gets' it. If the OP doesn't grasp it after that last explanation then my recommendation becomes: "Drop programming and take up cooking"
Was This Post Helpful? 1
  • +
  • -

#7 Ailaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 10-December 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 04:04 PM

View PosttlhIn`toq, on 07 January 2012 - 03:52 PM, said:

What's nice is the OP is already taking that into consideration. Nice to see someone not hardcoding values.


4 IntPic=rand.Next(0, Imagelist1.Images.Count

But to be honest, it worries me when such a simple bit of logic has to be repeated in increasingly more verbose detail until the OP 'gets' it. If the OP doesn't grasp it after that last explanation then my recommendation becomes: "Drop programming and take up cooking"


Thanks. Lol...I've grasped it...I won't take up cooking just yet :D

View Postsmohd, on 07 January 2012 - 03:07 PM, said:

What is he saying is as follows:
- Put your images in a list
- Randomly generate a number between 0 to the size of the list
- Use the number generated as an index to the list to get that image
- After using image, remove the image at that index
- Again generate another number and follow as above

What you need to remember is that in every loop the size of the list is decreasing, so your random generator must consider that.
Look at post #6 in the tutorial below and you will see an example:
http://www.dreaminco...random-numbers/


Many Thanks smohd ...I'll go and apply this. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 05:20 PM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 05:56 PM

It helps if think of real tangible objects.

I have a bag containing a collection bought items.
While the bag is not empty, take an item at random from the bag and put item away.
Was This Post Helpful? 1
  • +
  • -

#10 Ailaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 10-December 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 06:00 PM

Thanks Adam...that's exactly how I saw the problem...I thought Boolean array might be a way to do this initially

View PostAdamSpeight2008, on 07 January 2012 - 05:56 PM, said:

It helps if think of real tangible objects.

I have a bag containing a collection bought items.
While the bag is not empty, take an item at random from the bag and put item away.

Was This Post Helpful? 0
  • +
  • -

#11 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 06:08 PM

View PostAilaz, on 08 January 2012 - 11:00 AM, said:

Thanks Adam...that's exactly how I saw the problem...I thought Boolean array might be a way to do this initially

View PostAdamSpeight2008, on 07 January 2012 - 05:56 PM, said:

It helps if think of real tangible objects.

I have a bag containing a collection bought items.
While the bag is not empty, take an item at random from the bag and put item away.

So you have a list of images. One by one, remove the images and check the count. You could use a Do While loop if what you're doing is what I'm thinking:
Do While Not <list of images>.Count = 0
'randomly generate number in between the bounds of the array
'display image
'remove the item at the specified index: <list of images>.Remove(<list of images>.Item(<random number>)
Loop


But if you want them to be displayed you'd need the loop to run every little while because the loop runs so fast you won't actually see anything.

Aah dang. smohd already said something like this.
But, there is this link:
http://www.dreaminco...ers-no-repeats/

This post has been edited by jimmyBo: 07 January 2012 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 Ailaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 10-December 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 06:15 PM

:sleepy: This is the way I've done it so far.


Public Class Form1
    Dim intPic As Integer
    Dim pickedImg As New List(Of Integer)
    Dim rand As New Random

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 6000
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If pickedImg.Count < 1 Then 'if list count = less than 1 load image   
            chooseImg()

        End If
        Timer1.Start()

    End Sub

    Sub chooseImg()
        Dim i As Integer = ImageList1.Images.Count
        i = 10
        ' Dim rand As New Random 'move   
        intPic = rand.Next(0, i)

        If pickedImg.Count >= i Then 'if list count = max number of images, exit with message   
            Timer1.Stop()
            MessageBox.Show("End")
            Exit Sub
        End If
        If pickedImg.Contains(intPic) Then
            chooseImg() 'if list contains intPic, choose new random   
        Else
            pickedImg.Add(intPic)
            ListBox1.Items.Add(intPic.ToString) 'added for testing   
        End If

        PictureBox1.Image = ImageList1.Images(intPic)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        chooseImg()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Stop()
    End Sub


End Class

Was This Post Helpful? 0
  • +
  • -

#13 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 06:17 PM

And? Does it work?
Was This Post Helpful? 0
  • +
  • -

#14 Ailaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 10-December 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 06:19 PM

Basically I have five five buttons with five static images these remain static as each image is loaded in picturebox1. These act as a set of 'Image Grading scales (rather like the good to poor or 1-5 questionnaires). If the user clicks the appropriate image/ button then their response will need to be an integer 1-5 (each corresponding to a button).

The user will actually 'grade 20 images before the going to another form with another set of 'Image grading scales'.

As for the responses - at the beginning I have put to gether a login page and their details are saved in an excel worksheet. The responses to each random image will also need to go in here. The user will send the excel sheet back to me. Why ? They will take the test again and I looking at the intra-user and inter-user repeatbility of using these scales.

Hmm Am I making sense...I though using VB.NET would be a sophisticated way of doing the above as opposed to send out 100's of images for users to grade...learning lots in the process
Was This Post Helpful? 0
  • +
  • -

#15 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Problem Randomise images no repeats

Posted 07 January 2012 - 06:20 PM

You can set the interval of the timer in the Designer... you don't have to do it programatically.
But this part:

Dim i As Integer = ImageList1.Images.Count
        i = 10


Confuses me. Why set the value of i to ImageList1's image count, then immediately after set it to 10? What are you achieving here?
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3