Prev: ANUSHKA HOT PICTURES
Next: TextMate Common Lisp Bundle
From: Haris Bogdanović on 1 Aug 2010 11:22 Hi. How to randomize a list ? Thanks
From: Norbert_Paul on 1 Aug 2010 11:51 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 1 Aug 2010 11:53 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 1 Aug 2010 12:25 I mean like having a list of numbers from 1 to 10 and then randomize their order in list.
From: Udyant Wig on 1 Aug 2010 14:35
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). |