PDA

View Full Version : [SOLVED:] Random sentence generator



Programmer_n
04-13-2016, 05:38 AM
Friends,

The following code creates a random sentence every time it runs. Is there an elegant way of achieving the same?

It is too hard-coded. I would like to declare the s(5) as an array and call it from the array. By this way, I can bring in more random elements into the sentence.


Sub Randomsentence()
Dim MyText As String
Dim s(5) As String
Dim i As Integer
s(1) = "open."
s(2) = "closed."
s(3) = "broken."
s(4) = "fixed."
MyText = "The door is "
i = Int(4 * Rnd())
Selection.TypeText MyText & s(i)
End Sub

gmaxey
04-13-2016, 10:39 AM
Sub Randomsentence()
Dim arrWords()
Dim strText As String
Dim lngIndex As Long
arrWords = Array("open", "closed", "broken", "fixed", "stuck", "rusted", "off it's hinges")
Randomize
strText = "The door is "
lngIndex = Int((UBound(arrWords) + 1) * Rnd)
Selection.TypeText strText & arrWords(lngIndex)
lbl_Exit:
Exit Sub
End Sub