Results 1 to 8 of 8

Thread: Random Words

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    UK - Liverpool
    Posts
    113

    Arrow Random Words

    Hi, i really need to get a random word from a txt file. my txt file looks a little somthing like this...

    line one
    another line
    and other
    .....etc etc

    i had a look on here andi found this....

    Code:
    'Load the file into vWords
    Dim vWords() As String
    Dim iCount As Integer
    
    iCount = 0
    
    Open "Dictionary.txt" For Input As #1
    Do Until EOF(1)
        Line Input #1, tmp
        Redim vWords(iCount)
        vWords(iCount) = tmp
        iCount = iCount + 1
    Loop 
    Close #1
    
    ''Get a random value
    Dim I As Integer
    
    I = Int(Rnd * UBound(vWords)) + 1
    MsgBox "The Random word is: " & vWord(I)
    By Megtron
    and i think its what i need but it dont work for me

    wen i placed it in Form_Load and hit run it highlights the very last word on the last line (the vWord) and says..
    Complie Error:
    Sub or Function not defined
    maybe its because im using VB5? i dunno knowing me its prob somthing really silly
    thanx for reading

  2. #2

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    UK - Liverpool
    Posts
    113
    Hey thanx doh! its past that bit now but now when i hit the run button it just says The Random word is: and not returning any word can anyone spot anything else?
    thanx

  4. #4
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Hmm I've done this with a few things in my game. How about this:
    VB Code:
    1. Dim rndWords() As String
    2. Dim sFile As String
    3. Dim FF As Integer
    4.  
    5. Randomize
    6. FF = FreeFile()
    7. Open "C:\Words.Txt" For Input As #FF
    8.     sFile = Input$(LOF (FF), FF)
    9. Close #FF
    10.  
    11. rndWords = Split(sFile, vbCrLf)
    12.  
    13. MsgBox "The Random Word is: " & rndWords(int(rnd * UBound(rndWords)))
    -Excalibur

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    UK - Liverpool
    Posts
    113
    Thanx but is there anyway of doing it with out the spilt funcation? as im using VB5

  6. #6
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Sure, try this:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim Fred() As String
    3.     Dim X As Integer
    4.     Fred = vb5Split("this" & vbCrLf & "is" & vbCrLf & "only" & vbCrLf & "a" & vbCrLf & "test", vbCrLf)
    5.     For X = 0 To UBound(Fred)
    6.         MsgBox Fred(X)
    7.     Next X
    8. End Sub
    9.  
    10. Public Function vb5Split(ByVal sText As String, sDelim As String) As String()
    11.     Dim sTemp() As String
    12.     Dim X As Integer
    13.  
    14.     X = -1
    15.     sText = sText & sDelim
    16.     Do While InStr(1, sText, sDelim) > 0
    17.         X = X + 1
    18.         ReDim Preserve sTemp(X)
    19.         sTemp(X) = Left(sText, InStr(1, sText, sDelim) - 1)
    20.         sText = Mid(sText, Len(sTemp(X)) + Len(sDelim) + 1)
    21.     Loop
    22.     vb5Split = sTemp
    23. End Function
    There you have it a split function. The Form_Load Junk is an example of how to use it. Have fun.
    -Excalibur

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    UK - Liverpool
    Posts
    113

    Thumbs up

    Hey it worked thanx so much!

  8. #8
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Nop A Troblem
    -Excalibur

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