Hmm. (time isn't that reliable here. my lazy-shuffle might be quite
slow after all.

On Jan 26, 10:46 pm, bOR_ <boris.sch...@gmail.com> wrote:
> In addition to the functional shuffle thread (can't seem to post to
> that one anymore, might be too old?), I've written alazy shuffle. Not
> sure if it is the best way to write it, but I needed alazy shuffle
> primarily because I wanted to randomly pair a few agents from a large
> vector of agents without agents occurring one more than once.
>
> http://groups.google.com/group/clojure/browse_thread/thread/180842eb5...
>
> (defnlazy-shuffle
>  [coll]
>  (let [rand-pos (rand-int (count coll))
>        [prior remainder] (split-at rand-pos coll)]
>    (lazy-cons (nth coll rand-pos) (lazy-shuffle(concat prior (rest
> remainder))))))
>
> user=> (time (take 50 (lazy-shuffle(range 10000))))
>
> "Elapsed time: 0.246 msecs"
> user=> (time (take 50 (shuffle-java (range 10000))))
>
> "Elapsed time: 0.573 msecs"
>
> (defn frequencies
>          "Returns a map from distinct items in coll to the number
>           of times they appear."
>          [coll]
>         (reduce (fn [counts x]
>                   (merge-with + counts {x 1}))
>                  {} coll))
>
> (frequencies (map (fn [n] (take 3 (lazy-shuffle(list 1 2 3)))) (range
> 1000000)))
> {(3 1 2) 166687, (1 2 3) 166126, (1 3 2) 167246, (3 2 1) 166918, (2 3
> 1) 166687, (2 1 3) 166336}
--~--~---------~--~----~------------~-------~--~----~
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
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