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
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
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))
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!
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)
Re: Getting random words from text file or string.
Quote:
Originally Posted by
Oblio
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)
Re: Getting random words from text file or string.
Quote:
Originally Posted by
dglienna
Don't you HATE it when that happens. (catch 'ya next time)
Do you post at VBForums?
Re: Getting random words from text file or string.
Quote:
Originally Posted by
dglienna
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.
Re: Getting random words from text file or string.
Quote:
Originally Posted by
ddclondon
We're in a forum where we can exchange ideas.
Some suggestions are more elegant than others of course.
Well Said! :thumb: