From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: random predicate function Date: Mon, 13 Dec 2010 16:26:03 +0100 Organization: Informatimago Message-ID: <87mxo9r8xg.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1292260255 28885 80.91.229.12 (13 Dec 2010 17:10:55 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 13 Dec 2010 17:10:55 +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 18:10:50 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 1PSBvG-0007mj-12 for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Dec 2010 18:10:50 +0100 Original-Received: from localhost ([127.0.0.1]:55013 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PSBvF-0003LR-Ac for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Dec 2010 12:10:49 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 40 Original-X-Trace: individual.net Tx3M4G7MiTWW9yvaE8zuDAoc2eFdwN+vVjupOqyZUazualzLuj Cancel-Lock: sha1:YTI3MDBiMTA5YWZjOGRiNGI2MzdjZDM5OTBmMGMzMTJkOTc4ZWFmMA== sha1:10YHF8Po/WGFhbNXr/IyqUbnqXc= 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:183201 X-Mailman-Approved-At: Mon, 13 Dec 2010 12:00:28 -0500 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:77455 Archived-At: Tyler Smith writes: > Hi, > > I'm trying to write a function that will randomly sort the paragraphs in > a region. It seems to work ok, except that it doesn't seem very random. > I think the function I'm using to randomly generate true and false > values is sub-optimal. It often generates long strings of 't' or nil, > such that either the paragraph order doesn't change at all for multiple calls > to the function, or it simply reverses the order each time I call it. > > Any suggestions welcome! Thanks. > > Tyler > > Here is the function: 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. Google for: shuffle algorithm. 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. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}.