Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Alan Malloy
On Sep 26, 2:12 pm, Paul Richards wrote: > On Sep 26, 2:12 pm, Paul Richards wrote: > > > Hi, > > How can I efficiently pick a random element from a sorted-set? > > I've come up with a bit of a hack, which relies on me not caring about > non-uniform distributions.  If I create a custom comparator

Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Benny Tsai
On Monday, September 26, 2011 2:58:59 PM UTC-6, Paul Richards wrote: > > This will replace an O(n) call to "nth" with an O(n) call to copy into > a vector, so still leaving me with O(n). > Oops, right :) If you're getting random elements out of the same sorted set multiple times, then it might

Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Paul Richards
On Sep 26, 6:04 pm, Benny Tsai wrote: > The reason that (rand-nth (seq (sorted-set 1 2 3))) performs badly on large > sets is probably because nth is O(n) on sequences.  nth is much much faster > on vectors, so I would suggest trying out (rand-nth (vec (sorted-set 1 2 > 3))) and see if that works

Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Paul Richards
On Sep 26, 6:13 pm, Jeremy Heiler wrote: > On Mon, Sep 26, 2011 at 9:12 AM, Paul Richards > wrote: > > Hi, > > How can I efficiently pick a random element from a sorted-set? > > > If I try rand-nth I get this: > > user=> (rand-nth (sorted-set 1 2 3)) > > java.lang.UnsupportedOperationException:

Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Paul Richards
On Sep 26, 2:12 pm, Paul Richards wrote: > Hi, > How can I efficiently pick a random element from a sorted-set? > I've come up with a bit of a hack, which relies on me not caring about non-uniform distributions. If I create a custom comparator with a "random" backdoor, I can select random elemen

Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Jeremy Heiler
On Mon, Sep 26, 2011 at 9:12 AM, Paul Richards wrote: > Hi, > How can I efficiently pick a random element from a sorted-set? > > If I try rand-nth I get this: > user=> (rand-nth (sorted-set 1 2 3)) > java.lang.UnsupportedOperationException: nth not supported on this > type: PersistentTreeSet (NO_S

Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Benny Tsai
The reason that (rand-nth (seq (sorted-set 1 2 3))) performs badly on large sets is probably because nth is O(n) on sequences. nth is much much faster on vectors, so I would suggest trying out (rand-nth (vec (sorted-set 1 2 3))) and see if that works for your application. -- You received this

Re: Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Michael Gardner
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 ma

Randomly select an element from a sorted-set (rand-nth (sorted-set ..))

2011-09-26 Thread Paul Richards
Hi, How can I efficiently pick a random element from a sorted-set? If I try rand-nth I get this: user=> (rand-nth (sorted-set 1 2 3)) java.lang.UnsupportedOperationException: nth not supported on this type: PersistentTreeSet (NO_SOURCE_FILE:0) I can get this expression to work if I naively apply