[Small-cl-src] shuffle-vector

(defun shuffle-vector (v) "Return copy of vector with elements shuffled like a deck of cards." (loop with result = (copy-seq v) finally (return result) for i from (length v) downto 1 as j = (random i) do (rotatef (svref result j) (svref result (1- i))))) That was fun.

I like having both destructive and non-destructive versions: (defun shuffle-vector (v) "Return copy of vector with elements shuffled like a deck of cards." (shuffle-vector! (copy-seq v))) (defun shuffle-vector! (result) "Destructively shuffle elements in a vector like a deck of cards." (loop finally (return result) for i from (length result) downto 1 as j = (random i) do (rotatef (svref result j) (svref result (1- i))))) On Jun 24, 2004, at 10:03 AM, Ben Hyde wrote:
(defun shuffle-vector (v) "Return copy of vector with elements shuffled like a deck of cards." (loop with result = (copy-seq v) finally (return result) for i from (length v) downto 1 as j = (random i) do (rotatef (svref result j) (svref result (1- i)))))
That was fun.
_______________________________________________ Small-cl-src mailing list Small-cl-src@hexapodia.net http://www.hexapodia.net/mailman/listinfo/small-cl-src
-- Gary Warren King, Lab Manager EKSL East, University of Massachusetts * 413 577 0176 I believe that we can effectively defend ourselves abroad and at home without dimming our principles. Indeed, I believe that our success in defending ourselves depends precisely on not giving up what we stand for. -- Al Gore
participants (2)
-
Ben Hyde
-
Gary King