help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

random predicate function


From: Tyler Smith
Subject: random predicate function
Date: Mon, 13 Dec 2010 09:43:27 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]