2 Replies - 911 Views - Last Post: 09 May 2013 - 11:09 AM Rate Topic: -----

#1 Quiza12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-June 12

Randomly placing ALL images in an imagelist

Posted 07 May 2013 - 02:59 AM

Hi there guys,

I have made an image list and have a random number generator that is used to pull images with the same number index from the imagelist and have it placed on a grid.

My problem lies with the some images being called twice. I have eight images and wish to use all of them, and have them randomly generated.

My code so far:

Public Class Form1
    Dim rand As New Random
    Dim intPic As Integer
    Dim ImageList As ImageList
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        intPic = rand.Next(0, 8) 'pick random number

        PictureBox1.Image = ImageList.Images(intPic) 'choose image using the index number generated

        intPic = rand.Next(0, 8)

        PictureBox2.Image = ImageList.Images(intPic)

        intPic = rand.Next(0, 8)

        PictureBox3.Image = ImageList.Images(intPic)

        intPic = rand.Next(0, 8)

        PictureBox4.Image = ImageList.Images(intPic)

        intPic = rand.Next(0, 8)

        PictureBox5.Image = ImageList.Images(intPic)

        intPic = rand.Next(0, 8)

        PictureBox6.Image = ImageList.Images(intPic)

        intPic = rand.Next(0, 8)

        PictureBox7.Image = ImageList.Images(intPic)

        intPic = rand.Next(0, 8)

        PictureBox8.Image = ImageList.Images(intPic)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ImageList = New ImageList()
    End Sub
End Class

Is This A Good Question/Topic? 0
  • +

Replies To: Randomly placing ALL images in an imagelist

#2 maj3091  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 310
  • Posts: 1,849
  • Joined: 26-March 09

Re: Randomly placing ALL images in an imagelist

Posted 07 May 2013 - 03:45 AM

Keep a record of the ones you've already used. When you call your RNG routine, check if the number has been used and if it has call RNG again, until you get one that hasn't.

Would be easier if you wrapped your RNG in a function, so that it just returns a valid number to you and it does all the checking inside that function as whether the number has been used before.

Also, this VB.net, not VB6 code.

This post has been edited by maj3091: 07 May 2013 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 dday9  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 87
  • Joined: 17-April 13

Re: Randomly placing ALL images in an imagelist

Posted 09 May 2013 - 11:09 AM

The easiest way that I know of is like this:
        'New random and imgList(should be at the form_level)
        Dim imgList As New List(Of Image)
        Dim r As New Random

        'Not necessary, but makes the LINQ function look better IMO
        Dim upper_bound As Integer = imgList.Count - 1

        'Create an array of exclusive numbers from 0 to the imgList's count - 1
        Dim rnd_numbers() As Integer = Enumerable.Range(0, upper_bound).OrderBy(Function(n) r.Next()).ToArray()

Then set picturebox1-8 to imgList.Item(0 - 7).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1