unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: TomSW <tom.weissmann@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Random number generation in LISP or using it
Date: Thu, 11 Jun 2009 00:34:12 -0700 (PDT)	[thread overview]
Message-ID: <df29cafa-26c4-4e03-9983-fbcbbe3dd4c8@w3g2000yqf.googlegroups.com> (raw)
In-Reply-To: 0c194ee2-6347-4a46-8932-37d144e9265a@z7g2000vbh.googlegroups.com

On Jun 10, 5:21 pm, bolega <gnuist...@gmail.com> wrote:
> I am a newbie with the following problem.
>
> A the outset let me state that the limitation is that I have to use
> this inside emacs to scramble a set of chosen lines like you do
> "reverse-region" or "sort-lines". It is possible that I can call some
> scheme or clisp functions.

Grab the lines into an array, shuffle the array, re-insert the
lines :)

hth
Tom SW

------

(require 'cl) ; provides useful things like loop and setf

(defun shuffle-vector (vector)
  "Destructively shuffle the contents of VECTOR and return it."
  (loop
     for pos from (1- (length vector)) downto 1
     for swap = (random (1+ pos))
     unless (= pos swap)
     do (rotatef (aref vector pos)
                 (aref vector swap)))
  vector)

(defun randomize-region (start end)
  "Randomly re-order the lines in the region."
  (interactive "r")
  (save-excursion
    (save-restriction
      ;; narrow to the region
      (narrow-to-region start end)
      (goto-char (point-min))
      (let* ((nlines (line-number-at-pos end))
             (lines (make-vector nlines nil)))
        ;;
        (while (not (eobp))
          (setf (aref lines (decf nlines)) ; if it's random backwards
is fine
                (delete-and-extract-region (point)
                                           (progn (forward-visible-
line 1)
                                                  (point)))))
        ;;
        (let ((rlines (shuffle-vector lines)))
          (dotimes (linenum (length rlines))
            (insert (aref rlines linenum))))))))


  parent reply	other threads:[~2009-06-11  7:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-10 15:21 Random number generation in LISP or using it bolega
2009-06-10 15:26 ` bolega
2009-06-10 18:04 ` Colin S. Miller
2009-06-10 18:56   ` Lennart Borgman
2009-06-10 18:21 ` Thomas A. Russ
2009-06-10 18:21 ` Thomas A. Russ
2009-06-10 21:57 ` Xah Lee
2009-06-11  7:34 ` TomSW [this message]
2009-07-08 11:54 ` Mario Lang
2009-07-08 12:57   ` Pascal J. Bourguignon

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=df29cafa-26c4-4e03-9983-fbcbbe3dd4c8@w3g2000yqf.googlegroups.com \
    --to=tom.weissmann@gmail.com \
    --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).