 |

01-12-2012, 12:25 PM
|
|
Newcomer
|
|
Join Date: Jan 2012
Posts: 4
|
|
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
|
|

01-13-2012, 06:51 AM
|
|
Contributor
|
|
Join Date: Sep 2005
Posts: 565
|
|
|
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.
|
|

01-13-2012, 12:48 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
|
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.
|

01-14-2012, 12:35 AM
|
|
Newcomer
|
|
Join Date: Jan 2012
Posts: 4
|
|
Quote:
Originally Posted by passel
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
|
|

01-15-2012, 11:23 AM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
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.
|

01-16-2012, 05:43 AM
|
|
Newcomer
|
|
Join Date: Jan 2012
Posts: 4
|
|
|
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
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|