[erlang-questions] random lookup in ets

Pascal Chapier <>
Mon Aug 23 07:05:40 CEST 2010


I guess that your ets key is not an integer list :o). I imagine that you 
can do 2 things:

first, if your application manage the ets, you can add an new table as 
index, with an integer list as key, and the main ets key as value. then 
you will have to select a random integer between min and max index key, 
retreive the main ets key and then the value. The problem with this is 
that you will have to keep the index keys continous, it is not so easy 
and may takes some time when you make a delete operation. On the other 
hand it should be fast for random read.

second, you can use ets:tab2list(Ets), and ets:info(Ets,size), generate 
X with random:uniform(Size): a random value between 1 and Size, and 
finally take the Xth element of the list with lists:nth(X)..

hope this will help you.

Best regards

More information about the erlang-questions mailing list