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
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
* 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
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...
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
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