Err Posted April 3, 2007 Share Posted April 3, 2007 Hello.I'm working on a Visual Basic project where I have to use an array of strings that generate random sentences in a list box using the same strings. For example, if I was using the words "the", "cat", and "died" in the strings and it came out to "the cat died" the first time, the program would have to rewrite a different sentence each time using the same words when I click on a button. I have the basic things down, I'm just having trouble figuring out how to generating random sentences each time. Can I please get some help?If anyone wants the full code, I will gladly provide it. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click Dim str(5) As String str(0) = "cat " str(1) = "ate" str(2) = "in " str(3) = "dog" str(4) = "hat" str(5) = "the " lstOutput.Items.Add(str(5) & str(0) & str(2) & str(5) & str(4))End Sub Link to comment Share on other sites More sharing options...
aspnetguy Posted April 3, 2007 Share Posted April 3, 2007 you will need to generate random numbers to use as array indexes http://www.vbexplorer.com/VBExplorer/rando...m_numbers_1.asp Link to comment Share on other sites More sharing options...
jesh Posted April 3, 2007 Share Posted April 3, 2007 you will need to generate random numbers to use as array indexes http://www.vbexplorer.com/VBExplorer/rando...m_numbers_1.asp You could also keep a record of which random sequences you recently generated to avoid repetition. Link to comment Share on other sites More sharing options...
Err Posted April 25, 2007 Author Share Posted April 25, 2007 Sorry for the long wait in the reply. Thanks for your replies and links, I was able to figure it out. Link to comment Share on other sites More sharing options...
Yahweh Posted April 25, 2007 Share Posted April 25, 2007 You could also keep a record of which random sequences you recently generated to avoid repetition.Its a lot easier to work with an ArrayList, and a lot more efficient because you only have to loop through the elements of the ArrayList once to generate a random sentence.Here's some code that will do the job:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cArray As New Collections.ArrayList Dim sBuilder As New System.Text.StringBuilder For Each sItem As String In ListBox1.Items cArray.Add(sItem) Next Dim iCounter As Integer, iIterations As Integer Dim myRandom As New Random, iRnd As Integer iIterations = cArray.Count - 1 For iCounter = 0 To iIterations iRnd = myRandom.Next(0, cArray.Count) sBuilder.Append(cArray(iRnd)).Append(" ") cArray.RemoveAt(iRnd) Next Label1.Text = sBuilder.ToString End Sub It works like this:- All the elements from the listbox are shoved into an ArrayList.- The program loops through the ArrayList, selects a random item, appends it to a stringBuilder, then removes that item from the ArrayList.-- Because I'm using an ArrayList, the array indexes are automatically "reset" whenever I remove an item, so there aren't any gaps in list when I remove something from the middle of the list.- After looping through everything, the stringBuilder is printed to screen. Link to comment Share on other sites More sharing options...
jesh Posted April 25, 2007 Share Posted April 25, 2007 Its a lot easier to work with an ArrayList, and a lot more efficient because you only have to loop through the elements of the ArrayList once to generate a random sentence.I was referring to multiple runs through the randomizing sequence. If the first time you run it it outputs "cat ate dog hat in the", you may want to add some logic so that the next time the sequence runs "cat ate dog hat in the" isn't output again. I know it's (pseudo-)random and the probability that the same sequence will immediately repeat itself is very low, but some applications may require that no recent sequences repeat themselves.I'm a big fan of ArrayLists though. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now