dcsimg
Results 1 to 5 of 5

Thread: trying to randomize an array without duplicates!!!!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    england
    Posts
    27

    trying to randomize an array without duplicates!!!!

    hey im trying to take information from one array randomly and put it into a different array that wont show any duplicates, then show the information in the second array in labels (using a control array for the labels). i think it might have to do with a for loop inside a for loop to stop duplications.

    all your views will be grateful, thanks in advance.

    will
    dont believe in all that you've been told!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: trying to randomize an array without duplicates!!!!

    Moved from the CodeBank
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: trying to randomize an array without duplicates!!!!

    Rather than randomly selecting from the array and checking for duplicates you can swap the array around randomly... Better still, rather than having two arrays holding the same data, you can use one array for your data and another array for the indexes.
    Code:
    Private Sub Form_Load()
    Dim Indx() As Long, MyArray() As String
    Dim i As Long
      Randomize
      ReDim MyArray(65 To 74)
      For i = 65 To 74
        MyArray(i) = Chr$(i) & " " 'A to J
      Next i
    
      RndIndex Indx, 74, 65
      For i = 65 To 74
        Debug.Print MyArray(Indx(i));
      Next i
    End Sub
    
    Private Sub RndIndex(ReturnArray() As Long, UpperBound As Long, Optional LowerBound As Long = 0)
      Dim i As Long, T As Long
      Dim R As Long, S As Long
      
      ReDim ReturnArray(LowerBound To UpperBound)
      T = UpperBound - LowerBound + 1
      
      For i = LowerBound To UpperBound
        ReturnArray(i) = i
      Next i
      
      For i = LowerBound To UpperBound
        R = Int(Rnd * T) + LowerBound
        S = ReturnArray(i)
        ReturnArray(i) = ReturnArray(R)
        ReturnArray(R) = S
      Next i
    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    england
    Posts
    27

    Re: trying to randomize an array without duplicates!!!!

    thanks, i will try this and get back to you, as im not at home, ill try it tommorow, thanxs

    will
    dont believe in all that you've been told!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: trying to randomize an array without duplicates!!!!

    If its a single dimension string array you can transfer the info to a collection using the array element string as the collection key... if key already exists in collection an error will occur, handle the error by skipping that array element and clearing err object... you will end up with a collection of unique strings which you can transfer to your labels.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width