#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Help Needed with Randomising


    Just wondering if anyone here will be able to help me with a problem im having on Visual Basic
    I have to create a program which will randomise images within a picturebox
    I have to import the images in using My Resources and then create an Array with them
    my problem is im not sure if im doing this right as when i go to randomise the array of images the program fails and will not work

    Anyone willing to help me if i post up my code? or give me any tips to do this
  2. #2
  3. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    It's hard to help you debug code we can't see, or even speculate on a randomization method which is not even described.

    Hypothetical way to do this with just a standard array (adapt to image as appropriate)

    Code:
    Sub RandomArray()
        ' assumes arrays start at 0 (option base 0)
        ' create three undimensioned arrays
        Dim SourceList() As String ' the original list
        Dim RandomList() As String ' the randomized list
        Dim SelectedList() As Long ' to keep track of random selections
        Dim NumberSelected As Long ' keep track of the number of picks
        Dim NumToSelect As Long    ' how many we are picking (ubound + 1)
        Dim ThisSelection As Long  ' for the random pick
        ' populate the source list
        SourceList = Split("ANT,BAT,CAT,DOG,ELEPHANT,FROG,GERBIL,HAMSTER,IGUANA", ",")
        ' redimension our other two
        NumToSelect = UBound(SourceList) + 1 ' the number of elements in our source array
        ReDim RandomList(0 To UBound(SourceList))
        ReDim SelectedList(0 To UBound(SourceList))
        Randomize ' if you don't do this your random sequence will be the same every time
        Do
            Do
                ThisSelection = Int(Rnd * NumToSelect) ' select a random number in the range
            Loop Until SelectedList(ThisSelection) = 0 ' loop until we find a number we haven't picked yet
            SelectedList(ThisSelection) = 1            ' mark this as picked now
            RandomList(NumberSelected) = SourceList(ThisSelection) ' copy the data from the random pick to output list
            NumberSelected = NumberSelected + 1 ' increment the pick counter
        Loop Until NumberSelected = NumToSelect        ' loop until all have been picked
      ' Just output the results to immediate pane/debug window
        For ThisSelection = 0 To UBound(SelectedList)
            Debug.Print RandomList(ThisSelection) & " ";
        Next
        Debug.Print
    End Sub
    That's a VB6/VBA you can run in Excel, Access, etc. Should be about the same for .Net

    The output will be like
    Code:
    CAT GERBIL DOG ELEPHANT HAMSTER IGUANA BAT FROG ANT 
    FROG ANT HAMSTER IGUANA CAT GERBIL DOG BAT ELEPHANT 
    ELEPHANT HAMSTER IGUANA FROG ANT GERBIL DOG BAT CAT
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0
    Originally Posted by medialint
    It's hard to help you debug code we can't see, or even speculate on a randomization method which is not even described. ]
    Your Method seems really complicated to me im a bit confused but will have another, this is what i have got


    Method for doing Random
    Dim random As New Random



    Image Array
    Dim imgPictures(3) As Image
    imgPictures(0) = My.Resources.Red
    imgPictures(1) = My.Resources.Blue
    imgPictures(2) = My.Resources.Black
    imgPictures(3) = My.Resources.Yellow




    Trying to randomise the images within the Picturebox - THE PROBLEM
    Dim imgReel1 As Integer = random.Next(0, imgPictures.Images.Count)
    PicBox1.Image = imgPictures.Images(imgReel1)

    Dim imgReel2 As Integer = random.Next(0, imgPictures.Images.Count)
    Picbox2.Image = imgPictures.Images(imgReel2)

    I cant seem to get this part above working, Can you take a look and see if you can spot why?
    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo