Random line from a text file

Looking to call random line from text file (dif approach than last top

Page 1 of 1

3 Replies - 8640 Views - Last Post: 09 November 2008 - 04:56 PM Rate Topic: -----

#1 TerraEnvy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 07-November 08

Random line from a text file

Posted 08 November 2008 - 10:57 PM

Okay Here is what I am trying to do
I have this script here:
Imports system.IO
Public Class Form3

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Step1.Click
		If File.Exists("sayings.txt") Then
			Dim ioFile As New StreamReader("sayings.txt")
			Dim ioLine As String 
			Dim ioLines As String 
			ioLine = ioFile.ReadLine
			ioLines = ioLine
			While Not ioLine = ""
				ioLine = ioFile.ReadLine
				ioLines = ioLines & vbCrLf & ioLine
			End While
			RichTextBox1.AppendText(ioLines)
			ioFile.Close()
		Else
			Dim ioFile As New StreamWriter("sayings.txt")

			ioFile.WriteLine("Chinese words here")
			ioFile.WriteLine("Chinese words here")
			ioFile.WriteLine("Chinese words here")
			ioFile.Close()
		End If

	End Sub



What it does is reads from file sayings.txt and when I press Button1 it copies everything from sayings.txt into a richtextbox.

now the problem is that I don't want everything, I just want one random line.

Any help getting it to do this is appreciated.
-Terra

Is This A Good Question/Topic? 0
  • +

Replies To: Random line from a text file

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Random line from a text file

Posted 09 November 2008 - 12:52 AM

Well, you might copy the file lines into an arraylist. The arraylist will give you the count of number of lines copied to it, which you can use as a limit to a call to a random number generator. Use the random number returned as the index into the arraylist to retrieve a random saying. ;)

This post has been edited by n8wxs: 09 November 2008 - 12:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Random line from a text file

Posted 09 November 2008 - 09:46 AM

What I would so here is use a generic list (List(Of String) to be exact). I would then read through each line of the text file adding them to our list.

I would then generate a random number no higher than the number of lines in the list, then grab that index from the list to display. Here's an example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Step1.Click
    If File.Exists("sayings.txt") Then
    	'StreamReader to read our file
        Dim ioFile As New StreamReader("sayings.txt")
        'Generic list for holding the lines
        Dim lines As New List(Of String)
        'Random class to generate our random number
        Dim rnd As New Random()
        'Variable to hold our random line number
        Dim line As Integer
        'Now we loop through each line of our text file
        'adding each line to our list
        While ioFile.Peek <> -1
        	lines.Add(ioFile.ReadLine())
        End While
        
        'Now we need a random number
        line = rnd.Next(lines.Count + 1)
        'Now write out the random line to the TextBox
        RichTextBox1.AppendText(lines(line).Trim())
        'Close our StreamReader
        ioFile.Close()
        'Dispose of the instance
        ioFile.Dispose()
    Else
        Dim ioFile As New StreamWriter("sayings.txt")

        ioFile.WriteLine("Chinese words here")
        ioFile.WriteLine("Chinese words here")
        ioFile.WriteLine("Chinese words here")
        ioFile.Close()
    End If
End Sub



Hope that helps :)
Was This Post Helpful? 1

#5 TerraEnvy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 07-November 08

Re: Random line from a text file

Posted 09 November 2008 - 04:56 PM

View PostPsychoCoder, on 9 Nov, 2008 - 08:46 AM, said:

What I would so here is use a generic list (List(Of String) to be exact). I would then read through each line of the text file adding them to our list.

I would then generate a random number no higher than the number of lines in the list, then grab that index from the list to display. Here's an example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Step1.Click
    If File.Exists("sayings.txt") Then
    	'StreamReader to read our file
        Dim ioFile As New StreamReader("sayings.txt")
        'Generic list for holding the lines
        Dim lines As New List(Of String)
        'Random class to generate our random number
        Dim rnd As New Random()
        'Variable to hold our random line number
        Dim line As Integer
        'Now we loop through each line of our text file
        'adding each line to our list
        While ioFile.Peek <> -1
        	lines.Add(ioFile.ReadLine())
        End While
        
        'Now we need a random number
        line = rnd.Next(lines.Count + 1)
        'Now write out the random line to the TextBox
        RichTextBox1.AppendText(lines(line).Trim())
        'Close our StreamReader
        ioFile.Close()
        'Dispose of the instance
        ioFile.Dispose()
    Else
        Dim ioFile As New StreamWriter("sayings.txt")

        ioFile.WriteLine("Chinese words here")
        ioFile.WriteLine("Chinese words here")
        ioFile.WriteLine("Chinese words here")
        ioFile.Close()
    End If
End Sub



Hope that helps :)


OMG that is exactly what I needed.
Thank you so much. With this particular way of doing it I can actually finish 3 of my current projects ^_^
Thanks again
-Terra

Edit: It's not essential but do you know of a way that I can make it so instead of just generating 1 line I can set a numericupdown to change the number of lines that are generated into the richtextbox?

This post has been edited by TerraEnvy: 09 November 2008 - 05:07 PM

Was This Post Helpful? 1

Page 1 of 1