Results 1 to 4 of 4

Thread: rnd string from array, no repeats

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Location
    i live where you don't
    Posts
    76

    rnd string from array, no repeats

    i have an array of strings. i want to keep track of which ones i've used so i don't use them again. so right now i have it so that it adds the string to a listbox. when it chooses a word, it checks to see if it's already in the listbox. if it's already there, it'll keep choosing another string from the array until it finds one that hasn't been used yet. the problem with this method is that sometimes it'll stall for a long time because vb keeps choosing the same numbers over and over. does anyone know of a better way?

  2. #2
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    Why dont you have 2 listboxes....one with the ones to choose and ones with the ones that have been chosen.

    When you use a string, move it from one listbox to the other, that way you will only select a new string from ones that havent been chosen. If you dont want 2 listboxes, use 2 collections
    There are 3 types of people in this world.........those that can count, and those that can't.

    Blobby

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Here is an other option. You could load the contents of the array into a collection and as the item is used remove it from the collection.
    VB Code:
    1. Option Explicit
    2. Dim strState(9) As String
    3. Dim colState As Collection
    4.  
    5. Private Sub Command1_Click()
    6. Dim i As Integer
    7.    
    8.     If colState.Count = 0 Then Exit Sub
    9.    
    10.     Randomize
    11.     i = (Int(Rnd() * colState.Count)) + 1
    12.    
    13.     Me.Print colState(i)
    14.     colState.Remove i
    15.  
    16.        
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20. Dim i As Integer
    21.     Me.AutoRedraw = True
    22.    
    23.     strState(0) = "Alabama"
    24.     strState(1) = "Alaska"
    25.     strState(2) = "Arizona"
    26.     strState(3) = "Arkansas"
    27.     strState(4) = "California"
    28.     strState(5) = "Colorado"
    29.     strState(6) = "Connecticut"
    30.     strState(7) = "Delaware"
    31.     strState(8) = "Florida"
    32.     strState(9) = "Georgia"
    33.    
    34.     Set colState = New Collection
    35.     For i = 0 To 9
    36.         colState.Add Item:=strState(i)
    37.     Next i
    38. End Sub
    Last edited by MarkT; Oct 26th, 2002 at 03:41 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Location
    i live where you don't
    Posts
    76
    oh yeah...i was doing it backwards. hm.. thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width