Results 1 to 6 of 6

Thread: Randomizing a list? (please see other details)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    395

    Talking Randomizing a list? (please see other details)

    Lets assume I have a list of 100 words that are in alphabetical order.

    I want to click a button called "Randomize"

    This button will mix the words so that the list is no longer in alphabetical order. Each time I click randomize it will mix up the list.

    Does anyone have any code or know how to do this?\


    Thank You!
    8 gigs/ram (hey why not)
    300 gig HD x2
    Windows XP 64

  2. #2
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Randomizing a list? (please see other details)

    My first pass...
    Dim strWords(100) as string
    'List1 contains the 100 words
    'Create random number generator 0 to 100
    '
    for a=0 to list1.listcount
    bb:
    b=int(Rnd(1*10)*100)
    if b > 100 then goto bb
    aa:
    if len(strWords(b)) > 0 then b=b+1 : goto aa
    strWords(b)=list1.list(a)
    next


    Well, something like that....my fast coding example

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

    Re: Randomizing a list? (please see other details)

    VB Code:
    1. Option Explicit
    2.  
    3. Private sWords() As String
    4.  
    5. Private Sub Form_Load()
    6. Dim cnt As Long
    7.  
    8.    Randomize
    9.    ReDim sWords(99)
    10.    For cnt = 0 To 99
    11.       sWords(cnt) = "word " & cnt
    12.    Next
    13.    
    14.    Call Shuffle(100)
    15.    Text1.Text = Join(sWords, vbCrLf)
    16. End Sub
    17.  
    18. Private Sub Shuffle(ByVal nCount As Long)  'nCount +1 is number of elements affected by shift
    19. Dim idxA As Long
    20. Dim idxB As Long
    21. Dim sTmp As String
    22. Dim cnt As Long
    23.  
    24.    'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    25.    idxA = Int((99 - 0 + 1) * Rnd + 0)
    26.    sTmp = sWords(idxA)  'init circular shift
    27.    For cnt = 1 To nCount   'do intermediate shifts
    28.       Do
    29.          idxB = Int((99 - 0 + 1) * Rnd + 0)
    30.       Loop Until idxA <> idxB  'so you dont get indices to same element
    31.       sWords(idxA) = sWords(idxB)
    32.  
    33.       idxA = idxB
    34.    Next
    35.    sWords(idxB) = sTmp  'finish circular shift
    36. End Sub
    Last edited by leinad31; Nov 30th, 2006 at 05:31 AM.

  4. #4
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: Randomizing a list? (please see other details)

    VB Code:
    1. Private Sub Form_Load()
    2. Dim myWords(5) As String
    3. myWords(0) = "ABC"
    4. myWords(1) = "DEF"
    5. myWords(2) = "GHI"
    6. myWords(3) = "JKL"
    7. myWords(4) = "MNO"
    8. Randomize
    9.  
    10. Dim i As Integer
    11. Dim tmpWord As String, rndNum As Integer
    12. For i = 0 To UBound(myWords) - 1
    13.     rndNum = CInt(Rnd * (UBound(myWords) - 1))
    14.     tmpWord = myWords(rndNum)
    15.     myWords(rndNum) = myWords(i)
    16.     myWords(i) = tmpWord
    17. Next
    18.  
    19. For i = 0 To UBound(myWords) - 1
    20.     Debug.Print myWords(i)
    21. Next
    22. End Sub

  5. #5
    Addicted Member
    Join Date
    Mar 2002
    Location
    St. Louis MO
    Posts
    129

    Re: Randomizing a list? (please see other details)

    Beleive it or not I do that sort (no pun intended) of thing all the time.

    Personally I do it by looping through the array and swapping my "current position" with a random one somewhere else in the list.

    Something like this:

    For i=1 to 100
    r=random number between 1 and 100
    temp=arrayvalue[i]
    arrayvalue[i]=arrayvalue[r]
    arrayvalue[r]=temp
    next i

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Randomizing a list? (please see other details)

    Here's how I'd do it.

    VB Code:
    1. Dim colRandom As New Collection
    2.     Dim strValues(99) As String
    3.     Dim intIndex As Integer
    4.    
    5.     Randomize
    6.     On Error Resume Next
    7.     Do Until colRandom.Count = 100
    8.         colRandom.Add Int(100 * Rnd)
    9.     Loop
    10.    
    11.     For intIndex = 0 To 99
    12.         strValues(intIndex) = List1.List(intIndex)
    13.     Next
    14.    
    15.     List1.Clear
    16.    
    17.     For intIndex = 1 To 100
    18.         List1.AddItem strValues(colRandom(intIndex))
    19.     Next

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