From: ······@yahoo.com
Subject: random_shuffle
Date: 
Message-ID: <c120e6fd-b0bc-4efb-9ae0-0eb89fac81fb@u18g2000pro.googlegroups.com>
Hello!

C++ has random_shuffle <http://www.cplusplus.com/reference/algorithm/
random_shuffle.html> which re-arranges the elements of a sequence
randomly.

Does Common Lisp standard specify an equivalent function?

Tuomas

From: TomSW
Subject: Re: random_shuffle
Date: 
Message-ID: <9bbb1d30-7b1c-4e93-a8d7-6d1c90268058@w39g2000prb.googlegroups.com>
On Feb 3, 1:30 pm, ······@yahoo.com wrote:
> Hello!
>
> C++ has random_shuffle <http://www.cplusplus.com/reference/algorithm/
> random_shuffle.html> which re-arranges the elements of a sequence
> randomly.
>
> Does Common Lisp standard specify an equivalent function?
>
> Tuomas

Have a look at the Alexandria library's SHUFFLE function.

Regards,
Tom SW
From: Madhu
Subject: Re: random_shuffle
Date: 
Message-ID: <m3ljsn4or6.fsf@moon.robolove.meer.net>
* TomSW <····································@w39g2000prb.googlegroups.com> :
Wrote on Tue, 3 Feb 2009 05:22:26 -0800 (PST):

|> C++ has random_shuffle <http://www.cplusplus.com/reference/algorithm/
|> random_shuffle.html> which re-arranges the elements of a sequence
|> randomly.
|>
|> Does Common Lisp standard specify an equivalent function?

No not in the standard.


| Have a look at the Alexandria library's SHUFFLE function.

Why? Whats wrong with (say)

(defun shuffle (seq &optional (k (length seq) k-supplied-p))
  "shuffle all of SEQ. Stop when k elements have been shuffled.
This generalizes Knuth's algorithm"	; <··············@merced.netfonds.no>
  (let ((n (length seq)))
    (dotimes (i k seq)
      (rotatef (elt seq i)(elt seq (+ i (random (- n i))))))))

--
Madhu
From: TomSW
Subject: Re: random_shuffle
Date: 
Message-ID: <4ecbb36c-dc74-4956-88cc-fdf31ed801e8@r15g2000prd.googlegroups.com>
On Feb 3, 2:46 pm, Madhu <·······@meer.net> wrote:

> | Have a look at the Alexandria library's SHUFFLE function.
>
> Why? Whats wrong with (say)

There's nothing particularly wrong with your implementation, it's just
that it's already been done. There's got to be a useful maxim in here
somewhere involving not rediscovering some basic technology...
From: Marco Antoniotti
Subject: Re: random_shuffle
Date: 
Message-ID: <24a52435-40d4-45e8-929d-145aba05daa4@d32g2000yqe.googlegroups.com>
On Feb 3, 3:35 pm, TomSW <·············@gmail.com> wrote:
> On Feb 3, 2:46 pm, Madhu <·······@meer.net> wrote:
>
> > | Have a look at the Alexandria library's SHUFFLE function.
>
> > Why? Whats wrong with (say)
>
> There's nothing particularly wrong with your implementation, it's just
> that it's already been done. There's got to be a useful maxim in here
> somewhere involving not rediscovering some basic technology...

Tell that to the Ruby guy :)


(OTOH, you do have a point for many Lispers, writer included :) ).

Cheers
--
Marco
From: ······@yahoo.com
Subject: Re: random_shuffle
Date: 
Message-ID: <520f4b8e-88a3-42b5-920d-d148ebd0f7a6@f3g2000yqf.googlegroups.com>
On Feb 3, 3:46 pm, Madhu <·······@meer.net> wrote:
> * TomSW <····································@w39g2000prb.googlegroups.com> :
> Wrote on Tue, 3 Feb 2009 05:22:26 -0800 (PST):
>
> |> C++ has random_shuffle <http://www.cplusplus.com/reference/algorithm/
> |> random_shuffle.html> which re-arranges the elements of a sequence
> |> randomly.
> |>
> |> Does Common Lisp standard specify an equivalent function?
>
> No not in the standard.
>
> | Have a look at the Alexandria library's SHUFFLE function.
>
> Why? Whats wrong with (say)
>
> (defun shuffle (seq &optional (k (length seq) k-supplied-p))
>   "shuffle all of SEQ. Stop when k elements have been shuffled.
> This generalizes Knuth's algorithm"        ; <··············@merced.netfonds.no>
>   (let ((n (length seq)))
>     (dotimes (i k seq)
>       (rotatef (elt seq i)(elt seq (+ i (random (- n i))))))))
>
> --
> Madhu

This worked fine. I am grateful. Thanks for your replies!

Tuomas