[Small-cl-src] shuffle-vector

Gary King gwking at cs.umass.edu
Thu Jun 24 15:47:32 UTC 2004


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 at 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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1369 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/small-cl-src/attachments/20040624/abd5aa41/attachment.bin>


More information about the Small-cl-src mailing list