unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* random predicate function
@ 2010-12-13 14:43 Tyler Smith
  2010-12-13 15:37 ` Gary
  0 siblings, 1 reply; 17+ messages in thread
From: Tyler Smith @ 2010-12-13 14:43 UTC (permalink / raw)
  To: help-gnu-emacs

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:

(defun randomize-paragraphs (beg end)
  "Sort paragraphs in region randomly.
Called from a program, there are two arguments:
BEG and END (region to sort)."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (sort-subr nil
		 (function
		  (lambda ()
		    (while (and (not (eobp)) (looking-at paragraph-separate))
		      (forward-line 1))))
		 'forward-paragraph
		 nil
		 nil
		 (lambda (tmp1 tmp2) (if (eq (mod (random) 2) 0)
					 t
				       nil))))))




^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2010-12-15 18:39 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [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
     [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

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).