Call random string

Need to press a button and have it call a random text line.

Page 1 of 1

3 Replies - 2403 Views - Last Post: 09 November 2008 - 08:25 PM Rate Topic: -----

#1 TerraEnvy   User is offline

  • New D.I.C Head

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

Call random string

Posted 07 November 2008 - 08:55 PM

I am making a program to help a friend study chinese for school.
so...
Okay so here is what I need.
I have 400 lines of text and I need it so that when I click a button it will bring up a random line from that text.
It gets more complicated though.
Using numericupdown to determine the number of lines generated.
So in example terms:

Look at attachment or read this
Page opens, on the top there is a numericupdown box that goes to 100.
underneath that is a button that says generate. underneath that is output which is a rich text box



Now what it is supposed to do is you set the numericupdown box to a number then click generate, then the number you chose in the numericupdown box is how many random lines appear in the output box.

If you can get me started I can finish it.
Thanks in advance
-Terra

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Call random string

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5510
  • View blog
  • Posts: 14,500
  • Joined: 18-April 07

Re: Call random string

Posted 07 November 2008 - 09:46 PM

Look at the idea of an array of strings. When the form starts up, you read the file (look up the StreamReader class and its readLine() method or the File class and its readAllLines method) into an array of strings.

Once you have the array of strings, you can setup a random number generator (look up the random class and its method next()) inside a loop which will then pluck strings from the array, placing them one by one into the rich text box.

You can keep track of the numbers generated too so that you can prevent duplicates being chosen and instead go back to selecting another random string.

:)
Was This Post Helpful? 0
  • +
  • -

#3 TerraEnvy   User is offline

  • New D.I.C Head

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

Re: Call random string

Posted 08 November 2008 - 05:00 PM

View PostMartyr2, on 7 Nov, 2008 - 08:46 PM, said:

Look at the idea of an array of strings. When the form starts up, you read the file (look up the StreamReader class and its readLine() method or the File class and its readAllLines method) into an array of strings.

Once you have the array of strings, you can setup a random number generator (look up the random class and its method next()) inside a loop which will then pluck strings from the array, placing them one by one into the rich text box.

You can keep track of the numbers generated too so that you can prevent duplicates being chosen and instead go back to selecting another random string.

:)

Thanks for your help. too be honest I didnt fully understand all of your post X_X
But I have managed to make it this far but now I have one problem.
When I press my button it pastes everything that is in the text file into my richtextbox. The problem I am having is how to change my code so that when I click it only puts 1 line at random.
Thanks for your help and the code I have so far is below.
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



-Terra

This post has been edited by TerraEnvy: 08 November 2008 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Call random string

Posted 09 November 2008 - 08:25 PM

Pretty close to what Martyr2 was saying, however I am not sure if you want to load the text file everytime they click the button, maybe move that code to the form load event. He also suggested that you store the lines from the file into an array, I used a generic List(Of String) in my example. Then when they click the button it randomly picks one of the lines from the array using the random.next. I also implemented a Using clause around the creation of the StreamReader, this will ensure the file is closed and disposed of even if an error occurs.

*Warning: I haven't test this code so might need some tweaks...

    Private ioLines As New List(Of String)
    Private r As New Random

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If File.Exists("sayings.txt") Then
            ioLines.Clear()
            Using ioFile As New StreamReader("sayings.txt")
                Do Until ioFile.EndOfStream
                    ioLines.Add(ioFile.ReadLine)
                Loop
            End Using
        Else
            'Don't like this in production code, so remove when not needed for testing
            Using ioFile As New StreamWriter("sayings.txt")
                ioFile.WriteLine("Chinese words here")
                ioFile.WriteLine("Chinese words here")
                ioFile.WriteLine("Chinese words here")
            End Using
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Step1.Click, Button1.Click
               RichTextBox1.AppendText(ioLines(r.Next(ioLines.Count)))
    End Sub


This post has been edited by magicmonkey: 09 November 2008 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1