Grab 10 random words from text file

Jali

Member
Joined
Mar 16, 2010
Messages
5
Programming Experience
Beginner
I have a text document. i want to click a button and 10 random words out of the 196000 words shown in a textbox or listbox.
 
Your profile is indeterminate so I will post a VB 2008 solution
VB.NET:
Private rnd As New Random

Private Sub Take10Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Take10Button.Click
    Dim words = IO.File.ReadAllText("document.txt").Split(" "c)
    Dim random10 = words.OrderBy(Function() rnd.Next).Take(10)
    Me.ResultTextBox.Lines = random10.ToArray
End Sub
 
Let say i have a button with the name and text button1. and i have a listbox with its name listbox1 and under listbox properties the scrollalwaysvisible i change it to true. how do i open a txt file? this is step 1.. and then i will add another button with its name and text button2 and another listbox with its name listbox2 and when i click the button it will randomly pick 10 words from the textfile. Can you teach me slowly by stating if i need an openfiledialog and everything:)
 
actually i can open the text file to my listbox1 already. but now i want to click a button and generate 10 random words from listbox1 to listbox2. can i know the coding at button 2 for this pleaseeeeeee
 
actually i can open the text file to my listbox1 already. but now i want to click a button and generate 10 random words from listbox1 to listbox2. can i know the coding at button 2 for this pleaseeeeeee

JohnH has already shown you how. Getting 10 random items from a ListBox is no different to getting 10 random words from a file. You simply order the list randomly and take the first 10 items.
 
Back
Top