unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Tyler Smith <tyler.smith@eku.edu>
To: help-gnu-emacs@gnu.org
Subject: Re: random predicate function
Date: Mon, 13 Dec 2010 13:17:04 -0500	[thread overview]
Message-ID: <87k4jdk067.fsf@guruji.demimonde> (raw)
In-Reply-To: 87mxo9r8xg.fsf@kuiper.lan.informatimago.com

"Pascal J. Bourguignon" <pjb@informatimago.com> 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




  parent reply	other threads:[~2010-12-13 18:17 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.0.1292251427.11097.help-gnu-emacs@gnu.org>
2010-12-13 15:26 ` random predicate function Pascal J. Bourguignon
2010-12-13 17:16   ` Ted Zlatanov
2010-12-13 17:48     ` Pascal J. Bourguignon
2010-12-15 14:51       ` Ted Zlatanov
2010-12-15 15:20         ` Pascal J. Bourguignon
2010-12-15 16:44           ` Ted Zlatanov
2010-12-15 17:28             ` Pascal J. Bourguignon
2010-12-15 18:39               ` Ted Zlatanov
2010-12-15 18:04             ` Drew Adams
2010-12-13 18:17   ` Tyler Smith [this message]
     [not found]   ` <mailman.2.1292264248.1009.help-gnu-emacs@gnu.org>
2010-12-13 18:38     ` Pascal J. Bourguignon
2010-12-13 19:05       ` Tyler Smith
2010-12-13 14:43 Tyler Smith
2010-12-13 15:37 ` Gary
2010-12-13 16:08   ` Tyler Smith
2010-12-13 16:16     ` Erik Iverson
2010-12-13 16:50       ` Tyler Smith

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k4jdk067.fsf@guruji.demimonde \
    --to=tyler.smith@eku.edu \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).