Consulting

Results 1 to 5 of 5

Thread: random unique images

  1. #1

    random unique images

    Hello everyone,
    I am looking for help.... I would like to generate automaticly 15 random and unique Pictures for Image...
    so far I have this code, It can eveń overwrite some Images but not the one which was correct before the wrong one... I feel i Need Little push up...

    Private Sub CommandButton1_Click()
    Dim x As Integer
    Dim counter As Integer
    Dim priznak(1 To 15) As Integer
    counter = 1
    Randomize
    x = Int(15 * Rnd) + 1
    For i = 1 To 15
        If i = 1 Then
            Me("Image" & i).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & x & ".jpg")
            Me("Image" & i).Tag = x
            priznak(i) = x
        ElseIf i > 1 Then
            Me("Image" & i).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & x & ".jpg")
            Me("Image" & i).Tag = x
            priznak(i) = x
            For j = 1 To i - 1
            If priznak(j) = priznak(i) Then
                MsgBox "Already exist"
            End If
                Do While priznak(j) = priznak(i)
                x = Int(15 * Rnd) + 1
                Me("Image" & i).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & x & ".jpg")
                Me("Image" & i).Tag = x
                priznak(i) = x
                Loop
            Next
        End If
        x = Int(15 * Rnd) + 1
    Next
    End Sub
    everyones help would be welcome
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    series of 15 random unique numbers

    sub M_snb()
      [A1:A15]="=rand()"
      sn=[index(rank(A1:A15,A1:A15),)]
    end sub

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I don't know what
    correct before the wrong one
    means.

    Maybe:
    Sub test_RndIntPick()  
      Dim a() As Variant, i As Integer
      
      a() = RndIntPick(1, 15, 15)
      'MsgBox Join(a(), vbLf)
      For i = 1 To UBound(a)
        Me("Image" & i).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & a(i) & ".jpg")
        'MsgBox a(i)
      Next i
    End Sub
    
    Function RndIntPick(first As Long, last As Long, _
      noPick As Long, Optional bSort As Boolean = False) As Variant
      Dim i As Long, R As Long, temp As Long, k As Long
      ReDim iArr(first To last) As Variant
      Dim a() As Variant
      
      For i = first To last
        iArr(i) = i
      Next i
      
      Randomize
      For i = 1 To noPick
          R = Int(Rnd() * (last - first + 1 - (i - 1))) + (first + (i - 1))
          temp = iArr(R)
          iArr(R) = iArr(first + i - 1)
          iArr(first + i - 1) = temp
      Next i
      
      ReDim Preserve iArr(first To first + noPick - 1)
      ReDim a(1 To noPick)
      For R = 1 To noPick
        a(R) = iArr(LBound(iArr) + R - 1)
      Next R
      
      If bSort = True Then
        RndIntPick = ArrayListSort(a())
        Else
        RndIntPick = a()
      End If
    End Function
    
    Function ArrayListSort(sn As Variant, Optional bAscending As Boolean = True)
      With CreateObject("System.Collections.ArrayList")
        Dim cl As Variant
        For Each cl In sn
            .Add cl
        Next
         
        .Sort 'Sort ascendending
        If bAscending = False Then .Reverse 'Sort and then Reverse to sort descending
        ArrayListSort = .toarray()
      End With
    End Function

  4. #4
    Sorry for late answer....
    Snb I think i have made bad description of my Problem, I have 15 Pictures and I want to merge this 15 Pictures to 15 Images randomly and each Picture should be shown only once...( at future it would be Memory game with 15 pairs of Cards, and I will find this pairs...) so your solution is exactly what I described before, but I wanted something different anyway thank you for your Response....

    Kenneth I have found my own solution by adding one for each Loop, if you are interested there is solution, which I hope is one of the correct Solutions ...
    Dim x As Integer
    Dim counter As Integer
    Dim priznak(1 To 15) As Integer
    counter = 1
    Randomize
    x = Int(15 * Rnd) + 1
    For i = 1 To 15
        If i = 1 Then
            Me("Image" & i).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & x & ".jpg")    'add first random picture to first image
            Me("Image" & i).Tag = x
            priznak(i) = x
        ElseIf i > 1 Then
            Me("Image" & i).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & x & ".jpg")    'add random picture to next images
            Me("Image" & i).Tag = x
            priznak(i) = x
            For Each cell In priznak
            For j = 1 To i - 1
                Do While priznak(j) = priznak(i)
                    x = Int(15 * Rnd) + 1
                    Me("Image" & i).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & x & ".jpg")
                    Me("Image" & i).Tag = x
                    priznak(i) = x
                Loop
            Next
            Next
        End If
        x = Int(15 * Rnd) + 1
    Next

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Just use:

    Sub M_snb() 
      [A1:A15]="=rand()" 
      sn=[index(rank(A1:A15,A1:A15),)]
    
      for j=1 to 15
        Me("Image" & j).Picture = LoadPicture("V:\CGC_DATA\Images\picture" & sn(j,1) & ".jpg")
      next
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •