Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Game Programming > selecting random number of lines from text file


Reply
 
Thread Tools Display Modes
  #1  
Old 01-12-2012, 12:25 PM
carlw61 carlw61 is offline
Newcomer
 
Join Date: Jan 2012
Posts: 4
Default selecting random number of lines from text file


Hi,

Im making a text based game that i need to randomly select different numbers of lines from a text file then pass them to a list box.

The text file is made in the following format:
20058,v,o,ED,95
20059,v,o,TI,95
20060,v,o,TI,95
20061,v,o,SL,95

First the code needes to identify the 4th element then randomly select lines from lines that contain that element. Im stuck on how to randomly select lines from that list.

This is the code i have so far.

Code:
Sub GoClick(sender As Object, e As EventArgs)
		ListBox1.Items.Clear()
		'clears listbox1
		
		Dim depotsel() As String
		depotsel = combodepots1.text.Split(",")
		'reads combopepots1 and splits selected itme
		Dim code As String
		dim name As string
		code = depotsel(0)
		name = depotsel(1)
		textBox1.Text = name
		'textbox1 shows selected depots name
		
	
		Dim SR As StreamReader
		SR = New StreamReader("loco.txt")
		'assign SR as loco.txt file
		
		
		Do While SR.Peek <> -1
			Dim locos() As String = SR.ReadLine.Split(",")
			Dim number As String
			Dim brake As String
			Dim heat As String
			Dim depot As String
			Dim points As String
			number = locos(0)
			brake = locos(1)
			heat = locos(2)
			depot = locos(3)
			points = locos(4)
			'splits loco file
			
		
			If code = depot Then listBox1.Items.Add(number & " " & brake & heat & " " & depot & " " & Points)
			'matches loco file to combodepots1 and populates listbox1
            
		Loop

Many thanks

Carl
Reply With Quote
  #2  
Old 01-13-2012, 06:51 AM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 565
Default

Try this pseudo code

1- Read all file lines in a string array using IO.File.ReadAllLines
2- Create random number between 0 and upper bound of file lines count
3- Check if the line index (created in step 2) contains the element you need
4- If it is contain the element, use it, otherwise go to step 2.
__________________
Visit my site
Reply With Quote
  #3  
Old 01-13-2012, 12:48 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,890
Default

Of course that pseudo code may select the same element more than once.
How many lines to you need to randomly select?
Do you care if the same line is used more than once?
If you select random lines, do you also desire random order?
For instance, if you didn't care how many lines were selected, or the order,
then the code you have could just "flip a coin", every time you find a match as you're going down your list, and if it is "heads", add it to the list, otherwise don't add it to the list.
You need more definition than just selecting a number of random lines.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #4  
Old 01-14-2012, 12:35 AM
carlw61 carlw61 is offline
Newcomer
 
Join Date: Jan 2012
Posts: 4
Default

Quote:
Originally Posted by passel View Post
Of course that pseudo code may select the same element more than once.
How many lines to you need to randomly select?
Do you care if the same line is used more than once?
If you select random lines, do you also desire random order?
For instance, if you didn't care how many lines were selected, or the order,
then the code you have could just "flip a coin", every time you find a match as you're going down your list, and if it is "heads", add it to the list, otherwise don't add it to the list.
You need more definition than just selecting a number of random lines.
The code needs to work out how many lines have a matching text to the 4th array then randomly select lines from that total no need for a maximum limit to that number.
The same line must only been show the once at a time, but It can then be shown again on a different button click.
The order from the text file must be preserved.

Hope this help define a little better what I'm after.

Thanks

Carl
Reply With Quote
  #5  
Old 01-15-2012, 11:23 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,890
Default

Well, if I'm interpreting your requirements correctly, then as I said, simply flip a coin in your existing code to decide if you add to the list or not.
That will pick a random number of random lines that match your criteria, but maintain the order they are found in the file.
Code:
'In the declarations area, create a Random object
Private coin as New Random()


'In your code where you add to the list, "flip the coin" and choose whether to add or not.
If code = depot Then 
  If coin.NextDouble > 0.5 Then
    listBox1.Items.Add(number & " " & brake & heat & " " & depot & " " & Points)
			'matches loco file to combodepots1 and populates listbox1
  End If
End If
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #6  
Old 01-16-2012, 05:43 AM
carlw61 carlw61 is offline
Newcomer
 
Join Date: Jan 2012
Posts: 4
Default

That is exactly what i want.

I assume i can alter the ration of how many are shown by changing the 0.5 either bigger or smaller?

Thanks

Carl
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->