all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Xah Lee <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Random number generation in LISP or using it
Date: Wed, 10 Jun 2009 14:57:39 -0700 (PDT)	[thread overview]
Message-ID: <4a993edc-7b68-4e1d-8b08-9eb432c0c54b@h28g2000yqd.googlegroups.com> (raw)
In-Reply-To: 0c194ee2-6347-4a46-8932-37d144e9265a@z7g2000vbh.googlegroups.com

On Jun 10, 8:21 am, 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.
>
> What is a fast(est) method to randomize lines based on the
> capabilities of emacs ?
>
> The function "random" does not work in emacs. I guess one get the
> start line and end line of a marked region and then use that to
> disorder (or re-order) the lines according to a list of random numbers
> generated ?
>
> 1 -> 5
> 2 -> 8
> 3 -> 6
> 4 -> 9
> 5 -> 1
> 6 -> 3
> 7 -> 2
> 8 -> 7
> 9 -> 4
>
> and then use these pairs to reorder the lines. I guess there is a need
> for one or two variables to hold the lines, or just a kill or the top
> line and go down a certain number of random steps and paste it there ?
> Then come back to the top and do the same thing. The type of capacity
> I am looking is to randomize about few hundred short lines of about 20
> to 30 characters.
>
> In this case one needs to generate a random number that is an integer
> and lies between the lines start and end of region. It can be done by
> some modulo type operation if available in emacs lisp.
>
> Thanks for any help.
> gnuist

your problem is very easily solved, in fact i have code you can mode a
bit to do exactly what you want.

The simplest solution, for your input size, is simply just to grab
your lines and save as list. Then, randomnize the list, then insert
them one by one. Probably a 20 min job.

i assume you know scheme or common lisp, just not familiar with emacs
lisp features.

To get your lines into list, as a example, see the grab-lines function
here:

• Elisp Lesson: Writing a google-earth Function
  http://xahlee.org/emacs/google-earth.html

or you can see this page, that deals with reading lines one at a time.

• How To Process A File Line By Line In Elisp
  http://xahlee.org/emacs/elisp_process_lines.html

basically, i'd just do this:

• move your cursor to beginning of first line. (Use search-backward on
\n\n.)
• Move your cursor to the beginning of first line. (move-beginning-of-
line)
• save cursor pos as pos1.
• move your cursor to end of line. move-end-of-line
• save cursor pos as pos2.
• use buffer-substring-no-properties to grab the line and push into a
list.
• Repeat above.
• Delete the lines in buffer if you haven't already.
• Once you have lines as a list, call random with list length as arg.
• remove that line from your list and insert into buffer.
• Repeat till you list has length 0.

  Xah
∑ http://xahlee.org/

  parent reply	other threads:[~2009-06-10 21:57 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 [this message]
2009-06-11  7:34 ` TomSW
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

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

  git send-email \
    --in-reply-to=4a993edc-7b68-4e1d-8b08-9eb432c0c54b@h28g2000yqd.googlegroups.com \
    --to=xahlee@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.