On Sep 26, 2011, at 8:12 AM, Paul Richards wrote:

> How can I efficiently pick a random element from a sorted-set?

If your sorted set is densely packed (if most possible values do appear in the 
set), then you could just pick a random value, see if it's in the set, and 
repeat until you find a match.

Otherwise, you could use a sorted-vector. I don't know if there's a good 
implementation out there, though, so you might have to roll your own.

Or you could use a sorted-set that can do a "random binary search", where it 
chooses between the first and second halves at random rather than by comparison 
with a particular value. Maybe you could subclass the Clojure sorted-set to do 
this, though I'm guessing it's not made for subclassing.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to