From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" <pjb@informatimago.com> Newsgroups: gmane.emacs.help Subject: Re: random predicate function Date: Mon, 13 Dec 2010 18:48:19 +0100 Organization: Informatimago Message-ID: <8762uxr2cc.fsf@kuiper.lan.informatimago.com> References: <mailman.0.1292251427.11097.help-gnu-emacs@gnu.org> <87mxo9r8xg.fsf@kuiper.lan.informatimago.com> <87r5dlmw4a.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1292265727 26230 80.91.229.12 (13 Dec 2010 18:42:07 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 13 Dec 2010 18:42:07 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Dec 13 19:42:03 2010 Return-path: <help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org> Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from <help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org>) id 1PSDLU-0000W3-60 for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Dec 2010 19:42:00 +0100 Original-Received: from localhost ([127.0.0.1]:38453 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PSDLT-0001RA-MM for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Dec 2010 13:41:59 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 48 Original-X-Trace: individual.net nu7KsE+bXwARFo+tNbh5+wy7m0C08iXMyz6rWrixbGPH2HwUhN Cancel-Lock: sha1:Mzc5MjJkNDRkYTcxZmFjNzY4NWZkMDYxNGJkNjI4NGZiMGY4OTgzNQ== sha1:dNlEpAwhA4ZI+/cPBOhIYGr2KPw= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:183218 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor <help-gnu-emacs.gnu.org> List-Unsubscribe: <http://lists.gnu.org/mailman/listinfo/help-gnu-emacs>, <mailto:help-gnu-emacs-request@gnu.org?subject=unsubscribe> List-Archive: <http://lists.gnu.org/archive/html/help-gnu-emacs> List-Post: <mailto:help-gnu-emacs@gnu.org> List-Help: <mailto:help-gnu-emacs-request@gnu.org?subject=help> List-Subscribe: <http://lists.gnu.org/mailman/listinfo/help-gnu-emacs>, <mailto:help-gnu-emacs-request@gnu.org?subject=subscribe> Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:77461 Archived-At: <http://permalink.gmane.org/gmane.emacs.help/77461> Ted Zlatanov <tzz@lifelogs.com> writes: > 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) This is a clear demonstration of the power of macros, and the goodness of Common Lisp which includes a more powerful set of predefined macros than any other remaining lisp. > I wonder if it should be moved out of cookie1.el. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}.