From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tyler Smith Newsgroups: gmane.emacs.help Subject: Re: random predicate function Date: Mon, 13 Dec 2010 13:17:04 -0500 Message-ID: <87k4jdk067.fsf@guruji.demimonde> References: <87mxo9r8xg.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1292265132 23020 80.91.229.12 (13 Dec 2010 18:32:12 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 13 Dec 2010 18:32:12 +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:32:08 2010 Return-path: 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 ) id 1PSDBo-0003KF-Cx for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Dec 2010 19:32:00 +0100 Original-Received: from localhost ([127.0.0.1]:37281 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PSCyH-0008M8-MW for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Dec 2010 13:18:01 -0500 Original-Received: from [140.186.70.92] (port=49928 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PSCxg-0007Pm-1b for help-gnu-emacs@gnu.org; Mon, 13 Dec 2010 13:17:26 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PSCxe-0004kM-HR for help-gnu-emacs@gnu.org; Mon, 13 Dec 2010 13:17:23 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:39513) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PSCxe-0004jh-AC for help-gnu-emacs@gnu.org; Mon, 13 Dec 2010 13:17:22 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1PSCxc-0004EU-3P for help-gnu-emacs@gnu.org; Mon, 13 Dec 2010 19:17:20 +0100 Original-Received: from 157.89.133.51 ([157.89.133.51]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 13 Dec 2010 19:17:20 +0100 Original-Received: from tyler.smith by 157.89.133.51 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 13 Dec 2010 19:17:20 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 30 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 157.89.133.51 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) Cancel-Lock: sha1:UYrmCltkw2HdPvtFNJxxq8GJsqE= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-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:77459 Archived-At: "Pascal J. Bourguignon" writes: > > You shoud not use sort to randomize, because it's suboptimal > [ O(n*log(n)) at best instead of O(n) ]. > > And foremost, you should not use a predicate that is not a total order > because this usually gives invalid results. I don't know what a 'total order' means. Is the result of the predicate invalid or the actual sorting? > > Google for: shuffle algorithm. Thanks. I knew there must be a name for what I was trying to do. > You could instead put your paragraphs in a vector and use: > > (defun shuffle (vector) > "Re-orders randomly the vector." > (loop > for i from (1- (length vector)) downto 1 > do (rotatef (aref vector i) (aref vector (random i))))) > > to shuffle them and then re-insert them. Thanks for this! Very helpful. Tyler