help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: random predicate function


From: Ted Zlatanov
Subject: Re: random predicate function
Date: Mon, 13 Dec 2010 11:16:21 -0600
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/24.0.50 (gnu/linux)

On Mon, 13 Dec 2010 16:26:03 +0100 "Pascal J. Bourguignon" 
<pjb@informatimago.com> wrote: 

PJB> You could instead put your paragraphs in a vector and use:

PJB> (defun shuffle (vector)
PJB>   "Re-orders randomly the vector."
PJB>   (loop
PJB>       for i from (1-  (length vector)) downto 1
PJB>       do (rotatef (aref vector i) (aref vector (random i)))))

PJB> to shuffle them and then re-insert them.

I noticed this function in lisp/play/cookie1.el which doesn't require
CL:

; Thanks to Ian G Batten <BattenIG@CS.BHAM.AC.UK>
; [of the University of Birmingham Computer Science Department]
; for the iterative version of this shuffle.
;
;;;###autoload
(defun shuffle-vector (vector)
  "Randomly permute the elements of VECTOR (all permutations equally likely)."
  (let ((i 0)
        j
        temp
        (len (length vector)))
    (while (< i len)
      (setq j (+ i (random (- len i))))
      (setq temp (aref vector i))
      (aset vector i (aref vector j))
      (aset vector j temp)
      (setq i (1+ i))))
  vector)

I wonder if it should be moved out of cookie1.el.

Ted


reply via email to

[Prev in Thread] Current Thread [Next in Thread]