My random story generator...cant get to loop

webguy55

Member
Joined
Feb 14, 2010
Messages
12
Programming Experience
Beginner
Hello,

I am using some arrays that are pre-filled and I am having a random number generated between 0-10. That random number will then go in to all 5 arrays and pull the item from that location. I think try to take the sentence that is generated into another string. Once that is complete I then go through the final array to pull all the sentences and concatenate them together.

Currently I've only been able to get 1 sentence to print out of 5...a fresh pair of eyes would prob see where I am going wrong...I've been at this for awhile and wouldn't mind some advice to where I am going wrong.

Thanks in advance!

VB.NET:
Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Clear.Click

        txt_StoryBox.Text = ""

    End Sub


    Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click

        Me.Close()

    End Sub


    Private Sub btn_Show_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Show.Click

        Dim finalSentence(5) As String

        Dim nouns() As String = {"monkey", "pig", "senator", "lawyer", "doctor", "flower", "grapefruit", "ghost", "alien", "kumquat"}
        Dim verbs() As String = {"walked", "complained", "jumped up and down", "slept", "juggled", "ate", "stomped laughed", "danced", "cried"}
        Dim adverbs() As String = {"loudly", "softly", "gently", "wildly", "gingerly", "carefully", "angrily", "happily", "sadly"}
        Dim adjectives() As String = {"obese", "skinny", "red-haired", "elderly", "purple", "prickly", "furry", "crooked", "reptillian", "childish"}
        Dim connectors() As String = {"It all started when", "To everyon's surprise", "Then", "In spite of this", "Finally"}

        For i As Integer = 0 To 5

            Randomize()
            Dim randomNumber As New Random
            Dim rn As Integer
            Dim holder As Integer = 0

            rn = randomNumber.Next(0, 9)

            finalSentence(holder) = connectors(rn) & " the " & adjectives(rn) & " " & nouns(rn) & " " & verbs(rn) & " " & adverbs(rn) & ". "
            holder = holder + 1

        Next i

        For r As Integer = 0 To 5

            Dim arrayLoc As Integer

            txt_StoryBox.Text &= finalSentence(arrayLoc)
            arrayLoc = arrayLoc + 1

        Next r


    End Sub

End Class
 
  • You start 'holder' variable inside the loop with value 0 each iteration. Use the 'i' iterator instead of 'holder'.
  • Same problem in 'For r' loop. Use r as indexer instead.
  • Do not create a new Random each loop, create only one instance that you use to generate all random numbers.
  • Your arrays have variable length (5-10 elements), so a random index of 8 would be out of range.
Also bear in mind that with this very limited range of numbers it is likely that the same number is generated twice, which would result in the exact same sentence since you use same index for all arrays. A better solution which also suits variable length arrays is to generate a random number for each of the "words" you pick for the sentence.
 
Thanks John for the help.

I have a couple questions:

Trying to think about how you mentioned I should probably do a random number for each array instead of just using the 1 random number for all. Any suggestions or examples you can show me? Would I have to create like 5 variables all that will hold a different random number each time?

And the arrays with 10 values only pull the words that make up the sentences...and the only reason I have the array of 5 is to just hold the 5 sentences that are made. Once those are done it should then just print the Array items. I don't know if that will cause issues, but that was the best way i thought of going about doing it.
 
Hello again,

I took some of your advice and I now get 5 lines printed..but they are all the same sentence hehe..I wonder if its from what you were talking about..me needing to do a random number for each word..but here is my code so far. So how would I got about getting a random number for each item array?

VB.NET:
Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Clear.Click

        txt_StoryBox.Text = ""

    End Sub


    Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click

        Me.Close()

    End Sub


    Private Sub btn_Show_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Show.Click

        Dim finalSentence(5) As String

        Dim nouns() As String = {"monkey", "pig", "senator", "lawyer", "doctor", "flower", "grapefruit", "ghost", "alien", "kumquat"}
        Dim verbs() As String = {"walked", "complained", "jumped up and down", "slept", "juggled", "ate", "stomped laughed", "danced", "cried"}
        Dim adverbs() As String = {"loudly", "softly", "gently", "wildly", "gingerly", "carefully", "angrily", "happily", "sadly"}
        Dim adjectives() As String = {"obese", "skinny", "red-haired", "elderly", "purple", "prickly", "furry", "crooked", "reptillian", "childish"}
        Dim connectors() As String = {"It all started when", "To everyon's surprise", "Then", "In spite of this", "Finally"}

        Randomize()


        For i As Integer = 0 To 5

            Dim randomNumber As New Random

            Dim rn As Integer

            rn = randomNumber.Next(0, 11)

            finalSentence(i) = connectors(rn) & " the " & adjectives(rn) & " " & nouns(rn) & " " & verbs(rn) & " " & adverbs(rn) & ". "

        Next i

        For r As Integer = 0 To 5

            txt_StoryBox.Text &= finalSentence(r)

        Next r


    End Sub

End Class
 
Think i figured out what you meant by the 5 array getting the higher number so i just created a new random number for that array...but i am still printing 5 of the same sentence :(

VB.NET:
Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Clear.Click

        txt_StoryBox.Text = ""

    End Sub


    Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click

        Me.Close()

    End Sub


    Private Sub btn_Show_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Show.Click

        Dim finalSentence(5) As String

        Dim nouns() As String = {"monkey", "pig", "senator", "lawyer", "doctor", "flower", "grapefruit", "ghost", "alien", "kumquat"}
        Dim verbs() As String = {"walked", "complained", "jumped up and down", "slept", "juggled", "ate", "stomped laughed", "danced", "cried"}
        Dim adverbs() As String = {"loudly", "softly", "gently", "wildly", "gingerly", "carefully", "angrily", "happily", "sadly"}
        Dim adjectives() As String = {"obese", "skinny", "red-haired", "elderly", "purple", "prickly", "furry", "crooked", "reptillian", "childish"}
        Dim connectors() As String = {"It all started when", "To everyon's surprise", "Then", "In spite of this", "Finally"}

        Randomize()


        For i As Integer = 0 To 5

            Dim randomNumber As New Random
            Dim randomNumber2 As New Random
            Dim rn As Integer
            Dim conRN As Integer


            rn = randomNumber.Next(0, 9)
            conRN = randomNumber.Next(0, 4)

            finalSentence(i) = connectors(conRN) & " the " & adjectives(rn) & " " & nouns(rn) & " " & verbs(rn) & " " & adverbs(rn) & ". "

        Next i

        For r As Integer = 0 To 5

            txt_StoryBox.Text &= finalSentence(r)

        Next r


    End Sub

End Class
 
you also have to fix this:
JohnH said:
Do not create a new Random each loop, create only one instance that you use to generate all random numbers.
 
The reason for Johns "fix":
The PSEUDO random number generator needs a "seed" value. Initializing it with the same seed each time, will result in the EXACT same sequence of "random" numbers.
When you do NOT supply a seed value yourself (As New Random()), the current system time will be used to create the seed value. Since the internal clock has a low resolution, it will not advance if you call New Random() in a loop that runs quick. To avoid this problem, you use ONE instance of Random() that you can create either on method level, class level or even application level. Which level you need, you can determine yourself, after you understand the idea of the "seed".
 
Back
Top