weighted rand-nth proposal

101 views
Skip to first unread message

Edward Knyshov

unread,
Nov 20, 2015, 12:47:52 PM11/20/15
to Clojure Dev
Hi.
Weighted selection over collection is quite often in different fields.
From cluster discovery to generation of procedural content in games.
What I would like to propose is a new double-arity rand-nth function with arguments [coll weight].
It's supposed to be used like that:

(def coll [{:weight 2 :value 1}
           
{:weight 5 :value 1}
           
{:weight 7 :value 1}])

(rand-nth coll :weight)

Any thoughts?


Linus Ericsson

unread,
Nov 20, 2015, 1:17:10 PM11/20/15
to cloju...@googlegroups.com
/Linus

--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.

Alex Miller

unread,
Nov 20, 2015, 1:19:14 PM11/20/15
to cloju...@googlegroups.com
I do not see a strong argument for this being in core. 

Ghadi Shayban

unread,
Nov 20, 2015, 4:13:00 PM11/20/15
to Clojure Dev
Edward,
I don't represent core but I agree with Alex.  This does come up a bunch (it was in Rich's old ants.clj) demo.  Little know, but here is a gist of an O(1) implementation of weighted rand using Vose's "alias method".

Edward Knyshov

unread,
Nov 21, 2015, 8:01:16 PM11/21/15
to Clojure Dev
Thank you for your answers. I appreciate it. Generator looks good. I just didn't know it exists.
Reply all
Reply to author
Forward
0 new messages