dictionary, item ad random

andrews

Well-known member
Joined
Nov 22, 2011
Messages
126
Programming Experience
5-10
My question, can I reach ad random an item in my dictionary?
And take then the value of the key of that item
something like : dim n as integer = rand.next(1,mydic.count +1)
or another way
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+

andrews

Well-known member
Joined
Nov 22, 2011
Messages
126
Programming Experience
5-10
John, thanks very much but if i understand i have also to maintain this key collection because i have to add and remove items .
Maybe i have not given the question on a good way. My problem is in fact :
The items are JUST a set of random integers, nothing more and I have to do (find ad random an item, add and remove) but millions many times.
I found that a dictionary is very fast for adding and removing to do that but taking an item ad random looks a problem for a fast time.
My question is thus :
With a set of integers and to find ad random an item, or add an item or remove an item so very fast as possible what should i use as method?
A list do that but it looks not very fast, are there other posibilities for doing this in a faster way.
Thanks for any response.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Firstly, I think you mean "at" rather than "ad".

As for the issue, are you saying that you don't actually want to store keys and values but simply a collection of random Integer values? If so then use a HashSet(Of Integer). It is like a cross between a List(Of Integer) and the Keys collection of a Dictionary. It has Add, Contains and Remove methods. The Add method won't add duplicates and returns False in that case.
 

andrews

Well-known member
Joined
Nov 22, 2011
Messages
126
Programming Experience
5-10
Very much thanks.
It is that i would have, namely something like Hashset and i have seen that it is very very fast!
Thanks again.
 
Top Bottom