Question Need to generate a list of random Numbers from A list of Numbers

inkedgfx

Well-known member
Joined
Sep 29, 2012
Messages
139
Location
USA
Programming Experience
Beginner
I know ...me again.....

I have a listbox which i fill with numbers from a text file(coded solution on my own)
now I have a listbox full of numbers...I remove the duplicates...here is the part I am stuck on...

I need to take the numbers in the listbox and pick 6 random numbers from the numbers in the listbox...here is what I have so far.I use the msgbox to text and show what i have...I dont know where to go from here....any help is appreciated as always

Dim i As Integer
Dim x As Integer
Dim arr As New ArrayList
Dim r As New Random()
Dim intNumbers() As Integer
Try
For x = 0 To 5
For i = 0 To ListNumbers.Items.Count - 1
arr.Add(ListNumbers.Items.Item(i))
Next i

MsgBox(intNumbers)
Next x
Catch ex As Exception
MsgBox("ERROR! , " & ex.Message)
End Try

InkedGFX
 
yes , I agree this question should have been posted in a new thread...sorry about that!

my split code is as follows,

Dim Path As String = Application.StartupPath & "\LotteryNumbers\WinningNumbers.txt"
        Dim stream_reader As New StreamReader(Path)
        Dim line As String
            line = stream_reader.ReadToEnd()
            Dim separator() As Char = New Char() {" ", ","c}
            Dim strSplitArr() As String = line.Split(separator)
          
           
            ListNumbers.Items.Clear()
            
           
            For Each arrStr As String In strSplitArr
                If IsNumeric(arrStr) = True Then
                    ListNumbers.Items.Add(arrStr.Trim())
                End If
            Next


this code gets the second number , not the first.... so if I have a string of numbers i.e 1,2,3,4,5
the code grabs the 2 3 4 and 5 not the 1

not sure why....

thank you

InkedGFX
 
Firstly, I don't quite see why you're splitting on spaces and commas. Secondly, what exactly do you mean by "grabs"? Presumably that first item is addressed by the loop but it fails the IsNumeric test. Have you checked whether that is the case or not? Don't just read your code. If you could pick out all the errors that way then you probably wouldn't have made them in the first place. Run your code and observe it in action. That's called debugging and it's an integral part of the development process. Only then will you know if your code does what you think it does. I'm guessing that this is a data issue, which you also won't see just by reading your code.
 
thank you again for your help....

Im splitting on spaces and commas because thats what is repeated in the string.actually there are no spaces , I thought if i added the space delimeter then the first number might be caught if there was a space before the number, but it isnt.I thought the split function in vb.net returns the data before the delimeter...? but this doesn't seem to be the case , if it were then the first number should be caught in this string - "1,2,3,4,5" the only numbers caught in this string using the split function are 2 3 and 4 not 1 or 5.

I have not debugged as you suggest..only because I don't quite know how.....I have been "just reading the code" to try to figure out why it isnt working.

I will read about breakpoints...I assume this is what you mean by watching my code in action....

InkedGFX
 
I added a breakpoint at the line of code for the IsNumeric(arrStr) = true then

seems that the isnumeric is seeing the date that is in the string....which is numeric..so it does as it should.....so I added a "-" after the date and added a new delimeter as "-" to the split function....now the code does what it is supposed to.......not sure if this is the best way to code this but it works......

would you have any suggestions for reading material...I would like to learn vb.net the correct way..as of now I go online and read whatever I need to to get the job done.I would like to learn from the ground up......

thanks for all your help....I appreciate it.

InkedGFX
 
Here's how I would have done this:
Dim filePath = IO.Path.Combine(Application.StartupPath, "LotteryNumbers\WinningNumbers.txt")
Dim data = IO.File.ReadAllText(filePath).Split(","c)
Dim number As Integer

For Each value In data
    If Integer.TryParse(value, number) Then
        ListNumbers.Items.Add(number)
    End If
Next
I can assure you that Split does indeed get the data before the first and after the last delimiter. If it's not working as expected then something else is at play here and that's why you need to debug.
 
Here's how I would have done this:
Dim filePath = IO.Path.Combine(Application.StartupPath, "LotteryNumbers\WinningNumbers.txt")
Dim data = IO.File.ReadAllText(filePath).Split(","c)
Dim number As Integer

For Each value In data
    If Integer.TryParse(value, number) Then
        ListNumbers.Items.Add(number)
    End If
Next
I can assure you that Split does indeed get the data before the first and after the last delimiter. If it's not working as expected then something else is at play here and that's why you need to debug.

this works the same as before...it doesnt get the first number , but does get the last....I think the date is messing things up..if I remove the date ..this code works perfectly.....the date is like this mm/dd/yyy then the numbers start..as follows...

mm/dd/yyy - 1,2,3,4,5

so even with the delimeter being only the comma...it still doesnt get the first number before the first comma, which would be "1" it gets "2 3 4 5"

so I need the second delimeter "-" to get the first number after the date.

InkedGFX
 
Well, of course that's messing things up. If you split by just the command then the first value will be "MM/dd/yyyy - 1" and that is obviously not a number. It is not clear from any of your previous posts that your data had a date at the beginning so you might want to think about posting more clearly in future.

Anyway, what you need to do is remove the date first and then you can work with just the numbers, e.g.
Dim filePath = IO.Path.Combine(Application.StartupPath, "LotteryNumbers\WinningNumbers.txt")
Dim text = IO.File.ReadAllText(filePath)

'Split on the dash, take the second part and split it on the commas.
Dim data = text.Split("-"c)(1).Split(","c)

Dim number As Integer
 
For Each value In data
    If Integer.TryParse(value, number) Then
        ListNumbers.Items.Add(number)
    End If
Next
That assumes that the data is valid, which is not safe to do. Production code would require a bit of validation.
 
yes , I will give detailed info in future posts...

I understand what I was doing wrong , now what is confusing me is "number" doesnt have a value...you dim as integer but how does the code know what the integer will be.....?

InkedGFX
 
thank you for the help...and the info..I will look into the Int32.TryParse method today....

InkedGFX
 
Back
Top