CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Posts
    7

    Getting random words from text file or string.

    Hi Guys,

    I am using VB2010 and currently have a random letter and number applicaation but for some reason students dont seem to have the ability to type these in correctly!

    Does anyone have some code to put words from text file or string into a text box.

    i currently have:

    '----------------------------
    Dim verbs As String() = {"sin", "beg", "stays", "wash", "clean" , "cups", "gate", "wood"}
    '----------------------------

    How do i put one of these to a textbox?

    tb_word.text = ??

    any help would be great thank you!

    Anthony

  2. #2
    Join Date
    Jan 2010
    Posts
    7

    Re: Getting random words from text file or string.

    Hi I now have the below but does not seem to work?

    -------------------
    Dim str As String() = {"sin", "beg", "stays", "wash", "clean"}
    Dim N As Integer = str.Length
    Dim rnd As New Random((Now.Hour * 3600 + Now.Minute * 60 +
    Now.Second) * 1000 + Now.Millisecond)
    Dim sb As New StringBuilder

    For l As Integer = 1 To len
    sb.Append(str(rnd.Next(0, N), 1))
    Next
    Return sb.ToString

    ----------------------------

    i get the error:

    Number of indices exceeds the number of dimensions of the indexed array.

    Thanks
    Anthony
    Last edited by anthonywalker; January 8th, 2010 at 10:21 AM.

  3. #3
    Join Date
    Apr 2008
    Posts
    82

    Re: Getting random words from text file or string.

    Code:
            Dim rndm As New Random 'place at Class level
    
            Dim verbs As String() = {"sin", "beg", "stays", "wash", "clean", "cups", "gate", "wood"}
            Dim selWD As String
            selWD = verbs(rndm.Next(0, verbs.Length))

  4. #4
    Join Date
    Jan 2010
    Posts
    38

    Re: Getting random words from text file or string.

    Hi Tony

    Here's something for you to think about!
    I have extrapolated the code more than necessary for ease of reading / understanding.
    Block copy the following and place it within a Button_Click event:

    Dim intWordLenth As Integer
    Dim intWordNumber As Integer
    Dim intMaxWords As Integer
    Dim intLowerBound As Integer
    Dim strSource As String
    Dim strResult As String

    ' Setup the word options
    strSource = "JanFebMarAprMayJunJulAugSepOctNovDec"
    ' Ensure that all words are padded with spaces to make them the same 'length'
    intWordLenth = 3
    ' The total number of words in your source string
    intMaxWords = 12
    ' Set the Lower Bound for the Random Number Generator Range
    intLowerBound = 1
    ' Initialize the random-number generator.
    Randomize()

    'Get a valid random number between 1 and intMaxWords
    intWordNumber = Int((intMaxWords - intLowerBound + 1) * Rnd() + 1)
    ' Extract the corresponding word
    strResult = Mid$(strSource, ((intWordNumber - 1) * intWordLenth + 1), intWordLenth)

    lblMonth.Text = strResult

    Enjoy!
    Last edited by ddclondon; January 22nd, 2010 at 01:54 AM.

  5. #5
    Join Date
    Apr 2008
    Posts
    82

    Re: Getting random words from text file or string.

    Code:
            Dim rndm As New Random 'place at Class level
    
            Dim verbs() As String = New String() {"sin", "beg", "stays", "wash", "clean", "cups", "gate", "wood"}
            Dim selWD As String
            selWD = verbs(rndm.Next(0, verbs.Length))
    @ddclondon

    Code:
            Dim strSource As String = "JanFebMarAprMayJunJulAugSepOctNovDec"
            selWD = strSource.Substring(rndm.Next(0, 12) * 3, 3)

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Getting random words from text file or string.

    Quote Originally Posted by Oblio View Post
    Code:
            Dim strSource As String = "JanFebMarAprMayJunJulAugSepOctNovDec"
            selWD = strSource.Substring(rndm.Next(0, 12) * 3, 3)
    Don't you HATE it when that happens. (catch 'ya next time)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Apr 2008
    Posts
    82

    Re: Getting random words from text file or string.

    Quote Originally Posted by dglienna View Post
    Don't you HATE it when that happens. (catch 'ya next time)
    Do you post at VBForums?

  8. #8
    Join Date
    Jan 2010
    Posts
    38

    Re: Getting random words from text file or string.

    Quote Originally Posted by dglienna View Post
    Don't you HATE it when that happens.
    Absolutely not.

    We're in a forum where we can exchange ideas.

    Some suggestions are more elegant than others of course.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Getting random words from text file or string.

    Quote Originally Posted by ddclondon View Post
    We're in a forum where we can exchange ideas.

    Some suggestions are more elegant than others of course.
    Well Said!

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

Featured