Results 1 to 6 of 6

Thread: randomizing a complete text box

  1. #1
    Guest

    Question

    hi,
    i have a text box filled with 'single line' words (each word is in a single line, one after the other).
    how can i randomize the words to change their order?
    or whats the easiest & fastest way to randomize their order?

    e.g.

    blah
    yada
    bob
    jack

    will be randomized to any order (example):
    jack
    blah
    yada
    bob

    well you get my point..
    thanks..

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Check out the RND Function it should give you an idea
    of how to go about doing this.


    Option Explicit

    Private Sub Command1_Click()
    Dim i As Integer
    Dim hold(0 To 4) As String
    hold(0) = "Bill"
    hold(1) = "Kelly"
    hold(2) = "Chris"
    hold(3) = "Ed"
    hold(4) = "Sammy"

    For i = 1 To 5 ' just to print them in the same order
    Picture1.Print hold(i)
    Next i

    'to print them randomly
    ' comment out the for loop if this line is used

    Picture1.Print hold(Int(5 * Rnd))
    End Sub

  3. #3
    Guest

    random text in text box

    well, as i said.. i have a text box, and i don't know how many words are in it..

    anyone?

  4. #4
    Guest
    You could do something like...

    have a listbox (hidden), textbox, and 2 command buttons.

    Command1 = Splits all words into an array/adds them to a list
    Command2 = randomizes list

    Code:
    Private Sub Command1_Click()
        Dim vArray As Variant
        Dim iArray As Integer
        
        vArray = Split(Text1.Text, vbCrLf)
        
        For iArray = 0 To UBound(vArray)
            List1.AddItem vArray(iArray)
        Next iArray
    End Sub
    
    Private Sub Command2_Click()
        Randomize
        MsgBox List1.List(Int(List1.ListCount * Rnd))
    End Sub
    ...just a suggestion .

  5. #5
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    This function seems to work, even though I'm sure it can be done more efficiently.....

    Code:
    Private Sub Form_Load()
     Dim s As String
     s = "what why when how where"
     Debug.Print RandomizeWords(s)
    End Sub
    
    Private Function RandomizeWords(s As String) As String
     Dim iPos As Integer, iPrevPos As Integer
     Dim sWords() As String
     Dim iWordCt As Integer
     Dim iCt As Integer
     Dim sTemp As String
     Dim iRandIndex As Integer
        
        'Only run function if string parameter is not empty
        If Not s = "" Then
        
            Do
            
                'get position of first space
                iPos = InStr(iPos + 1, s, " ")
                
                ' redimension the array to hold next word
                ReDim Preserve sWords(iWordCt)
                
                If Not iPos = 0 Then
                
                    ' get word
                    sWords(iWordCt) = Trim(Mid(s, iPrevPos + 1, iPos - iPrevPos))
                    ' increase word count to redimension array with enough values
                    iWordCt = iWordCt + 1
                    
                Else
                
                    'get the last word in the string
                    sWords(iWordCt) = Trim(Mid(s, iPrevPos + 1, Len(s) - iPrevPos))
              
               End If
               
                    iPrevPos = iPos
            
            Loop Until iPos = 0
            
            'rearrange the array
            For iCt = 0 To iWordCt
            
                Randomize ' assure rnd returns different value
                iRandIndex = (Rnd * iWordCt - 1) + 1 ' get a random index
                sTemp = sWords(LBound(sWords)) ' hold current value of first element in array
                sWords(LBound(sWords)) = sWords(iRandIndex) ' set first element to hold whatever random value
                sWords(iRandIndex) = sTemp ' set random element to hold value of first element
            
            Next iCt
            
            For iCt = LBound(sWords) To UBound(sWords)
                
                ' display the words with a space when a space is necessary
                If Not iCt = LBound(sWords) Then
                    RandomizeWords = RandomizeWords & " "
                End If
                
                RandomizeWords = RandomizeWords & sWords(iCt)
                
            Next iCt
            
        End If
    End Function
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  6. #6
    Guest

    ok, let me explain myself all over again

    i have a text box
    i load a file containing words into it
    then i load the words from the text to a list box
    then i use int(rnd*list.listcount) to get a name
    but i always get the same order of the names

    is there any other way to load the names from the text box to the list in a random order?
    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