From: Haris Bogdanović on
Hi.

How to randomize a list ?

Thanks
From: Norbert_Paul on
Haris Bogdanović wrote:
> How to randomize a list ?
Do you mean creating a random permutation?

A hint: Consider dotimes, elt, and cons.
From: Tamas K Papp on
On Sun, 01 Aug 2010 17:22:40 +0200, Haris Bogdanović wrote:

> Hi.
>
> How to randomize a list ?
>
> Thanks

If you want a random permutation of a list with all permutations
receiving equal probability, then you need

http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

Trivial to program. You might want to convert to a vector first if
your list is long. If you would prefer to use a library, check out
SHUFFLE in ALEXANDRIA.

Tamas
From: Haris Bogdanović on

I mean like having a list of numbers from 1 to 10
and then randomize their order in list.
From: Udyant Wig on

Do you use Emacs? Specifically, 'emms'?

This function found in emms.el for shuffling a vector
might prove useful:

(defun emms-shuffle-vector (vector)
"Shuffle VECTOR."
(let ((i (- (length vector) 1)))
(while (>= i 0)
(let* ((r (random (1+ i)))
(old (aref vector r)))
(aset vector r (aref vector i))
(aset vector i old))
(setq i (- i 1))))
vector)

I'll leave it up to you to translate this to Common Lisp.
Among other things, you'll want to replace the '(while ...' with a
'(loop for ...', and the two asets with a single
rotatef. You _could_ replace aref with nth (or elt).


 |  Next  |  Last
Pages: 1 2 3
Prev: ANUSHKA HOT PICTURES
Next: TextMate Common Lisp Bundle