Random Item of a Dictionary

  • Thread starter Thread starter Peter Gast
  • Start date Start date
P

Peter Gast

I need a function to get an item of a Dictionary randomly. Because Dicionary
hasn't an item-property like List (Of ..) I cannot pick the item with a
random index. The type of Dictionary can varies, so I have to declare it as
an object. It should also work for SortedDictionary.
The code above works, but I thinks object as type isn't good. Are there an
ideas for improvements?

Public Shared Function getElement(ByVal liste As Object) As Object
Dim position As Integer = CInt(Rnd() * (liste.Values.Count - 1))
Dim counter As Integer = 0
For Each item As Object In liste.values
If counter = position Then Return item
counter += 1
Next
End Function

Peter
 
Am 31.07.2011 23:37, schrieb Peter Gast:
I need a function to get an item of a Dictionary randomly. Because Dicionary
hasn't an item-property like List (Of ..) I cannot pick the item with a
random index. The type of Dictionary can varies, so I have to declare it as
an object. It should also work for SortedDictionary.
The code above works, but I thinks object as type isn't good. Are there an
ideas for improvements?

Public Shared Function getElement(ByVal liste As Object) As Object
Dim position As Integer = CInt(Rnd() * (liste.Values.Count - 1))
Dim counter As Integer = 0
For Each item As Object In liste.values
If counter = position Then Return item
counter += 1
Next
End Function

Peter

I'd use the generic IDictionary type for this:

Public Shared Function getElement(Of TKey, TValue)(ByVal liste As IDictionary(Of TKey, TValue)) As TValue

Dim position As Integer = Convert.ToInt32(Math.Floor(Rnd() * liste.Count))
Return liste.ElementAt(position).Value

End Function

- You could create an extension member from it.
- I've changed the conversion because it's the straighter/quicker way.

Call:
Dim dict As New Dictionary(Of Integer, String)
Dim o = getElement(dict)


But instead, I'd put the values also into a List(Of) or just an array
to get access by index. Quicker than a dictionary for this purpose.
 
Interfaces were the importint hint. This works very good

Thanks


"Armin Zingler" schrieb im Newsbeitrag

Am 31.07.2011 23:37, schrieb Peter Gast:
I need a function to get an item of a Dictionary randomly. Because
Dicionary
hasn't an item-property like List (Of ..) I cannot pick the item with a
random index. The type of Dictionary can varies, so I have to declare it
as
an object. It should also work for SortedDictionary.
The code above works, but I thinks object as type isn't good. Are there an
ideas for improvements?

Public Shared Function getElement(ByVal liste As Object) As Object
Dim position As Integer = CInt(Rnd() * (liste.Values.Count - 1))
Dim counter As Integer = 0
For Each item As Object In liste.values
If counter = position Then Return item
counter += 1
Next
End Function

Peter

I'd use the generic IDictionary type for this:

Public Shared Function getElement(Of TKey, TValue)(ByVal liste As
IDictionary(Of TKey, TValue)) As TValue

Dim position As Integer = Convert.ToInt32(Math.Floor(Rnd() *
liste.Count))
Return liste.ElementAt(position).Value

End Function

- You could create an extension member from it.
- I've changed the conversion because it's the straighter/quicker way.

Call:
Dim dict As New Dictionary(Of Integer, String)
Dim o = getElement(dict)


But instead, I'd put the values also into a List(Of) or just an array
to get access by index. Quicker than a dictionary for this purpose.
 
Back
Top