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
 
Check these out:

Unique, Random Selections from a List
Randomise a List of Items

Using that second option, once you have randomised the list, you can call its Take method to get the first N items. For instance, if you had 40 lottery numbers you could put them in a list, randomise it and then take the first 6 and you have a lottery draw.
 
thank you for the reply...I have one problem...this gets the index of the number in random...not the actual number...Im sure I am doing something wrong!
here is the code...

Dim list As New ArrayList

Try
For i As Integer = 0 To ListNumbers.Items.Count - 1
list.Add(i)
Next i
Dim r As New Random()
Dim index As Integer
Dim item As Object
While list.Count > 0
index = r.Next(0, list.Count)
item = list(index)
list.RemoveAt(index)
Label1.Text = item.ToString()
MsgBox(item.ToString())
End While

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

thank you
InkedGFX
 
thank you for your help, but I don't know where I am going wrong...I have looked at the for loop , I cant seem to figure out what is wrong......

i have changed this
For i As Integer = 0 To ListNumbers.Items.Count - 1
list.Add(i)
Next i

to this

For i As Integer = 1 To ListNumbers.Items.Count
list.Add(i)
Next i

no good ..still get the index number of the number

thank you for your help

InkedGFX
 
ok...I have thought about this all day at work today...( this is a hobby for me) I am a printer by trade...I am attempting to learn vb.net on my own.....

so I have the listbox already filled with the numbers from the txt file.....I figure I need two for loops ...one inside the other...like so

Dim i As Integer
Dim x As Integer
Dim numbers(5) As Integer
Dim r As New Random()
Dim index As Integer
Try
For i = 0 To 5 - 1
For x = 0 To ListNumbers.Items.Count - 1
numbers(index) = r.Next(ListNumbers.Items.Item(x))
Next x
Next i
Label1.Text = numbers(0)
Label2.Text = numbers(1)
Label3.Text = numbers(2)
Label4.Text = numbers(3)
Label5.Text = numbers(4)
Catch ex As Exception
MsgBox("ERROR! , " & ex.Message)
End Try

this kind of works..... but it generates some random numbers not in the listbox.....not sure whats wrong

thanks for any help

InkedGFX
 
Use an array to shuffle the numbers randomly

I think this is what you want:

VB.NET:
   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ndx As Integer = listnumbers.Items.Count - 1
        Dim randnum As New Random
        Dim num, mix, temp As Integer
        Dim listarray(ndx) As Integer

        num = 0        
        For Each itm As Integer In listnumbers.Items   'copy numbers from first listbox to an array
            listarray(num) = itm
            num +=1
        Next

        For x As Integer = 0 To ndx        'shuffle the array indexes randomly
            mix = randnum.Next(x, ndx)
            temp = listarray(mix)
            listarray(mix) = listarray(x)
            listarray(x) = temp
        Next x

        list.Items.Clear()            'display the first 6 numbers of the array in the 2nd listbox
        For x As Integer = 0 To 6
            list.Items.Add(listarray(x))
        Next
        
    End Sub
 
I need to add numbers from a list box to the list then fill a label with the number
I asked two questions and you only answered one. This is fairly simple stuff. You want to add the numbers from the ListBox, right? Is that what you're doing? No it is not. You are adding 'i'. Is 'i' a number from the ListBox? No, it's the loop counter. If you want an item then you have to use the loop counter as an index to get an item and then you can add it. As Solitaire has now demonstrated, a For Each loop is a more appropriate choice than a For loop in this case.
 
I appreciate both of you taking the time to help me learn....I dont really understand the difference from a for each loop as opposed to a for loop...dont they both count the items in the list either way?

for each itm in list1.items isnt the same as counting each itm with a for next loop?

Thank You
InkedGFX
 
If all you want is to access each item in a list then you should use a For Each loop, because that's exactly what it does. A For loop is not about accessing items in a list specifically. A For loop is about using a number whose value changes across a range. That number might be used as an index into a list but it doesn't have to be. It doesn;t make sense to use a For loop when all you want is to access lost items because the code for a For loop is more complex than for a For Each loop:
VB.NET:
For i = 0 To myList.Count - 1
    Dim item = myList(i)

    'Use item here.
Next
VB.NET:
For Each item In myList
    'Use item here.
Next
Those two loops both give you access to each item in myList in turn but the second one is obviously more succinct than the first. You should generally only use a For loop if you need to use the loop counter for some purpose other than accessing items in a single list. For instance, let's say that you had two lists and the items in each at the same index were related. A For Each loop can't handle more than one list so you'd use a For loop in that case. In each iteration you would index both lists using the loop counter to get the two related items.
 
one more thing before we go....

Im splitting a string by this char - "," but this doesnt get the first number in the string..for example

number string - 1,2,3,4,5,6
split string starts with 2 not 1

how do I get the first number in the string ?

thank you again
InkedGFX
 
Furthermore, this is a different question and should be in a new thread.

RE: For/For-Each loops:
A For-Each loop is read-only. It works with collections, such as arrays and listboxes. It will access every single index in a collection from 0 to the last one, so you don't need to know exactly what the last index is. But you cannot assign or change a value in that collection. A For loop can specify a range from the initial to the terminal value and also includes a Step option.
 
Last edited:
Back
Top