Random listbox item selection

K

kimiraikkonen

Hi,
How can i select next listbox item without obeying array order? How to
select an listbox item "randomly" inside the array?

Thanks.
 
F

Family Tree Mike

kimiraikkonen said:
Hi,
How can i select next listbox item without obeying array order? How to
select an listbox item "randomly" inside the array?

Thanks.

dim r as new Random
listBox1.SelectedIndex = r.Next ( 0, listBox1.Items.Count - 1 )
 
K

Kerry Moorman

Family Tree Mike,

I think that should be:

listBox1.SelectedIndex = r.Next ( 0, listBox1.Items.Count)

because the range of values returned by Next includes the minvalue but
excludes the maxvalue.

Kerry Moorman
 
K

kimiraikkonen

Family Tree Mike,

I think that should be:

listBox1.SelectedIndex = r.Next ( 0, listBox1.Items.Count)

because the range of values returned by Next includes the minvalue but
excludes the maxvalue.

Kerry Moorman

Thanks "Kerry Moorman" and "Family Tree Mike",
It worked. The last question is: how may i determine if all the
listbox items were selected randomly? I mean, after all the listbox
items are selected, i want to get a msgbox that all the listbox items
are selected randomly, there's no item left unselected(in random
order).(just like an information about end of list)

Thanks!
 
C

Charlie Brown

Thanks "Kerry Moorman" and "Family Tree Mike",
It worked. The last question is: how may i determine if all the
listbox items were selected randomly? I mean, after all the listbox
items are selected, i want to get a msgbox that all the listbox items
are selected randomly, there's no item left unselected(in random
order).(just like an information about end of list)

Thanks!- Hide quoted text -

- Show quoted text -

try something like

if listbox.items.count = listbox.selecteditems.count then
'do something
end if
 
K

kimiraikkonen

try something like

if listbox.items.count = listbox.selecteditems.count then
'do something
end if

Hi, that doesn't work, only works with listbox's multiselect
(multiselect at the same time) function which is offtopic. I want to 2
thins if anyone can help:

For random selection procedure(as Kerry and Family Tree Mike helped);
1- I want an listbox item selected only for once, i don't want a
listbox item selected for twice or three (random order).
2-When all the items are selected in a random order, i want to get a
msgbox("all items were selected, there is no item left unselected in
random order").

Thanks!
 
F

Family Tree Mike

kimiraikkonen said:
Hi, that doesn't work, only works with listbox's multiselect
(multiselect at the same time) function which is offtopic. I want to 2
thins if anyone can help:

For random selection procedure(as Kerry and Family Tree Mike helped);
1- I want an listbox item selected only for once, i don't want a
listbox item selected for twice or three (random order).
2-When all the items are selected in a random order, i want to get a
msgbox("all items were selected, there is no item left unselected in
random order").

Thanks!

I think you want something like this. the list loi should be a form level
member while the rest is in a routine where you are changing the selected
item. Somehow, depending on your requirements, you will need to know that
the routine could not pick any more entries.

Here is the code:

Dim loi As New System.Collections.Generic.List(Of Integer)

public bool NextRandomClick()
Dim r As New Random
Dim idx As Integer

If (loi.Count = ListBox1.Items.Count) Then
MsgBox("All have been selected...")
Return false
End If

idx = r.Next(0, ListBox1.Items.Count)

While (loi.Contains(idx))
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = r.Next(0, ListBox1.Items.Count - 1)
return true
end function
 
F

Family Tree Mike

Family Tree Mike said:
I think you want something like this. the list loi should be a form level
member while the rest is in a routine where you are changing the selected
item. Somehow, depending on your requirements, you will need to know that
the routine could not pick any more entries.

Here is the code:

Dim loi As New System.Collections.Generic.List(Of Integer)

public bool NextRandomClick()
Dim r As New Random
Dim idx As Integer

If (loi.Count = ListBox1.Items.Count) Then
MsgBox("All have been selected...")
Return false
End If

idx = r.Next(0, ListBox1.Items.Count)

While (loi.Contains(idx))
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = r.Next(0, ListBox1.Items.Count - 1)
return true
end function

Sorry, I pasted the bad version....

Replace the line:
ListBox1.SelectedIndex = r.Next(0, ListBox1.Items.Count - 1)
with:
ListBox1.SelectedIndex = idx
 
F

Family Tree Mike

Family Tree Mike said:
I think you want something like this. the list loi should be a form level
member while the rest is in a routine where you are changing the selected
item. Somehow, depending on your requirements, you will need to know that
the routine could not pick any more entries.

Here is the code:

Dim loi As New System.Collections.Generic.List(Of Integer)

public bool NextRandomClick()
Dim r As New Random
Dim idx As Integer

If (loi.Count = ListBox1.Items.Count) Then
MsgBox("All have been selected...")
Return false
End If

idx = r.Next(0, ListBox1.Items.Count)

While (loi.Contains(idx))
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = r.Next(0, ListBox1.Items.Count - 1)
return true
end function

All right... It's an early morning... I fixed it up som more...

Dim loi As New System.Collections.Generic.List(Of Integer)

public bool NextRandomClick()
Dim r As New Random
Dim idx As Integer

If (loi.Count = ListBox1.Items.Count) Then
MsgBox("All have been selected...")
Return false
End If

idx = r.Next(0, ListBox1.Items.Count)

While (loi.Contains(idx))
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = idx
loi.Add(idx)
return true
end function
 
K

kimiraikkonen

All right... It's an early morning... I fixed it up som more...

Dim loi As New System.Collections.Generic.List(Of Integer)

public bool NextRandomClick()
Dim r As New Random
Dim idx As Integer

If (loi.Count = ListBox1.Items.Count) Then
MsgBox("All have been selected...")
Return false
End If

idx = r.Next(0, ListBox1.Items.Count)

While (loi.Contains(idx))
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = idx
loi.Add(idx)
return true
end function

As your code seems a class library type, i optimized as to be used in
a executable but no help:

Dim r As New Random
Dim idx As Integer
Dim loi As New System.Collections.Generic.List(Of Integer)

If loi.Count = ListBox1.Items.Count Then
MsgBox("All have been selected...")

End If

idx = r.Next(0, ListBox1.Items.Count)

While loi.Contains(idx)
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = idx
loi.Add(idx)

Still, the selection goes endless,never ends. Once an item selected,
this(same) item is being selected somehow in next random selection.
 
K

kimiraikkonen

All right... It's an early morning... I fixed it up som more...

Dim loi As New System.Collections.Generic.List(Of Integer)

public bool NextRandomClick()
Dim r As New Random
Dim idx As Integer

If (loi.Count = ListBox1.Items.Count) Then
MsgBox("All have been selected...")
Return false
End If

idx = r.Next(0, ListBox1.Items.Count)

While (loi.Contains(idx))
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = idx
loi.Add(idx)
return true
end function

However, currently the thing that i want to implement is not to select
an listbox item more than once in random order. In second and third
posts, as Kerry and Mike helped, random selection code works good but
usually (not so frequently) it selects the "same" item for twice or
third consecutively on next selections. It seems the random selection
algorithm is not fascinating, that's why i want not to see an listbox
item selected more than once in random selection order. An item must
be selected only for "once" That's my main purpose. I hope you would
help :)

Thanks!
 
F

Family Tree Mike

As your code seems a class library type, i optimized as to be used in
a executable but no help:

Dim r As New Random
Dim idx As Integer
Dim loi As New System.Collections.Generic.List(Of Integer)

If loi.Count = ListBox1.Items.Count Then
MsgBox("All have been selected...")

End If

idx = r.Next(0, ListBox1.Items.Count)

While loi.Contains(idx)
idx = r.Next(0, ListBox1.Items.Count)
End While

ListBox1.SelectedIndex = idx
loi.Add(idx)

Still, the selection goes endless,never ends. Once an item selected,
this(same) item is being selected somehow in next random selection.

I suspect that is because the List (Of Integer) is reset each time into the
subroutine. That is why I made it a class member. It is getting recreated
on each entry into the routine.
 
K

kimiraikkonen

I suspect that is because the List (Of Integer) is reset each time into the
subroutine. That is why I made it a class member. It is getting recreated
on each entry into the routine.

I didn't like this random selection algorithm, it doesn't do %100 what
i wanted. As i stated in my previous post, a listbox item must be
selected for once consecutively, the next random item selection must
NOT be the previous one(self). My aim was a random list like playing
songs randomly(shuffle) in Windows Media Player which stops at the end
of random selection and which doesn't select same item more than once
in random(shuffle) mode.

Anyway thanks Mike for striving on this issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top