unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to create random characters?
@ 2003-12-14  9:59 Brad Collins
  2003-12-14 11:10 ` Eli Zaretskii
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Brad Collins @ 2003-12-14  9:59 UTC (permalink / raw)




I'm hoping there is a simple function in elisp to do this....

I want to generate a random id which is in the format of XXX-0000
where 'XXX' is made up of ascii characters a-z and 0000 is a random
number 0000-9999.

I found random -- so that (random 10000) produces the number very
nicely but I can't find anything that will produce three random
characters in the same way.

Is there a simple way of doing this that I haven't found?

Cheers,

b/

--
Brad Collins
Chenla Labs
Bangkok, Thailand

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

* Re: How to create random characters?
  2003-12-14  9:59 How to create random characters? Brad Collins
@ 2003-12-14 11:10 ` Eli Zaretskii
       [not found] ` <mailman.53.1071403922.868.help-gnu-emacs@gnu.org>
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2003-12-14 11:10 UTC (permalink / raw)


> From: Brad Collins <brad@studiojungle.net>
> Date: Sun, 14 Dec 2003 16:59:52 +0700
> 
> I want to generate a random id which is in the format of XXX-0000
> where 'XXX' is made up of ascii characters a-z and 0000 is a random
> number 0000-9999.
> 
> I found random -- so that (random 10000) produces the number very
> nicely but I can't find anything that will produce three random
> characters in the same way.

Characters are just small integers, so evaluating the following
expression for each of the 3 characters:

   (+ ?a (random (- ?z ?a)))

will give you 3 random numbers between the ASCII code of `a' and that
of `z'.

Does this solve your problem?

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

* Re: How to create random characters?
       [not found] ` <mailman.53.1071403922.868.help-gnu-emacs@gnu.org>
@ 2003-12-15  8:07   ` Joakim Hove
  2003-12-15  8:46     ` Eli Zaretskii
  0 siblings, 1 reply; 9+ messages in thread
From: Joakim Hove @ 2003-12-15  8:07 UTC (permalink / raw)



Slightly off-topic,

but what does the "?" in the expression below do?

> Characters are just small integers, so evaluating the following
> expression for each of the 3 characters:
>
>    (+ ?a (random (- ?z ?a)))

(I can guess - but without any previous experience from lisp I am a
 bit lost with the special character like "@", "#", "`" and "?" which
 some elisp code is filled with. Any tips of where to find these
 documented would be appreciated.)


Joakim 



-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove@bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/

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

* Re: How to create random characters?
  2003-12-15  8:07   ` Joakim Hove
@ 2003-12-15  8:46     ` Eli Zaretskii
  0 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2003-12-15  8:46 UTC (permalink / raw)


> From: Joakim Hove <hove@bccs.no>
> Newsgroups: gnu.emacs.help
> Date: Mon, 15 Dec 2003 09:07:18 +0100
> 
> but what does the "?" in the expression below do?

?x is the Lisp notation for the character x.

> (I can guess - but without any previous experience from lisp I am a
>  bit lost with the special character like "@", "#", "`" and "?" which
>  some elisp code is filled with. Any tips of where to find these
>  documented would be appreciated.)

They are documented in the ELisp manual.  See the node "Lisp Data
Types" there, and its sub-nodes.

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

* Re: How to create random characters?
  2003-12-14  9:59 How to create random characters? Brad Collins
  2003-12-14 11:10 ` Eli Zaretskii
       [not found] ` <mailman.53.1071403922.868.help-gnu-emacs@gnu.org>
@ 2003-12-18  9:22 ` Brad Collins
       [not found] ` <mailman.242.1071742690.868.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 9+ messages in thread
From: Brad Collins @ 2003-12-18  9:22 UTC (permalink / raw)


Brad Collins <brad@studiojungle.net> writes:

> I'm hoping there is a simple function in elisp to do this....
>
> I want to generate a random id which is in the format of XXX-0000
> where 'XXX' is made up of ascii characters a-z and 0000 is a random
> number 0000-9999.
>

Thanks to all who helped!  

I did find if you use Emacs' "random" function, it will always use the
same seed to generate random numbers.  You can use (random t) to use a
different seed, and get different random sequences every time, but my
function uses this argument as a limit.  So every time I would restart
Emacs I'd get the same sequence of random IDs.

So I switched to using the clisp function random* and it seems to work.

Here is the function I finally came up with.  There is probably a
better way of doing this, and yes, tt's trivial, but seems
to work and might be of use to someone.


(defun insert-bxid ()
  "Insert random Burr Exchange ID (BXID) at point.
   BXID's are based on the old telephone exchange 
   numbers which are easy to remember and can be used
   mnemonically.  For example: WAL5-9000 could be 
   remembered as Walnut Five--Nine Thousand.

   To deal with potential conflicts between duplicate
   ID's, BXID's are mapped to both a namespace 
   (xml style) which points to an Exchange Registry
   as well as a UUID which is universally unique."
  (interactive)
  (require 'cl)
  (insert
   (format "%c%c%c" 
     	   (+ ?A (random* (- ?Z ?A))) 
     	   (+ ?A (random* (- ?Z ?A))) 
     	   (+ ?A (random* (- ?Z ?A))))
   (format "%d" (random* 9))
   "-"
   (format "%d%d%d%d"
     (random* 9 )
     (random* 9 )
     (random* 9 )
     (random* 9))))

Thanks again!

b/

--
Brad Collins
Chenla Labs
Bangkok, Thailand

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

* Re: How to create random characters?
       [not found] ` <mailman.242.1071742690.868.help-gnu-emacs@gnu.org>
@ 2003-12-18 10:32   ` Kai Grossjohann
  2003-12-18 17:30     ` Brad Collins
       [not found]     ` <mailman.280.1071771962.868.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: Kai Grossjohann @ 2003-12-18 10:32 UTC (permalink / raw)


Brad Collins <brad@studiojungle.net> writes:

> I did find if you use Emacs' "random" function, it will always use the
> same seed to generate random numbers.  You can use (random t) to use a
> different seed, and get different random sequences every time, but my
> function uses this argument as a limit.

My understanding is that you call (random t) once, eg after Emacs
starts up or when your file is loaded.  After this, you can call
(random 42) to obtain numbers in the desired range.

Does it not work that way?  I think it's intended to work that way.
If it doesn't, that might be a bug.

Kai

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

* Re: How to create random characters?
  2003-12-18 10:32   ` Kai Grossjohann
@ 2003-12-18 17:30     ` Brad Collins
       [not found]     ` <mailman.280.1071771962.868.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 9+ messages in thread
From: Brad Collins @ 2003-12-18 17:30 UTC (permalink / raw)


Kai Grossjohann <kai@emptydomain.de> writes:

> My understanding is that you call (random t) once, eg after Emacs
> starts up or when your file is loaded.  After this, you can call
> (random 42) to obtain numbers in the desired range.
>
> Does it not work that way?  I think it's intended to work that way.
> If it doesn't, that might be a bug.

I don't think it's a bug...

,----[ Elisp Manual: Random Numbers ]
|    In Emacs, pseudo-random numbers are generated from a "seed" number.
| Starting from any given seed, the `random' function always generates
| the same sequence of numbers.  Emacs always starts with the same seed
| value, so the sequence of values of `random' is actually the same in
| each Emacs run!  For example, in one operating system, the first call
| to `(random)' after you start Emacs always returns -1457731, and the
| second one always returns -7692030.  This repeatability is helpful for
| debugging.
| 
|    If you want random numbers that don't always come out the same,
| execute `(random t)'.  This chooses a new seed based on the current
| time of day and on Emacs's process ID number.
`----

 When I evaluated (random t)

   I got 80264346

 But I need to get a single digit between 0-9 so I used (random 9)

 But this then uses the same seed every time so the sequence of
 numbers I'm returned will be repeated every time I restart Emacs.

 So I needed to limit the number of digits that were returned but at
 the same time make sure that it will always be random and not start
 giving me the same numbers I got the day before.

 I changed to the Clisp random* and things seem to work.

 Perhaps I've misunderstood this whole thing -- please correct me if
 I got this wrong... or why it's now working....  it wouldn't be the
 first time I got something to work the right way for the wrong
 reasons :)

b/

--
Brad Collins
Chenla Labs
Bangkok, Thailand

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

* Re: How to create random characters?
       [not found]     ` <mailman.280.1071771962.868.help-gnu-emacs@gnu.org>
@ 2003-12-18 18:30       ` Harald Maier
  2003-12-18 18:31       ` Kai Grossjohann
  1 sibling, 0 replies; 9+ messages in thread
From: Harald Maier @ 2003-12-18 18:30 UTC (permalink / raw)


Brad Collins <brad@studiojungle.net> writes:

> Kai Grossjohann <kai@emptydomain.de> writes:

> ,----[ Elisp Manual: Random Numbers ]
> |    In Emacs, pseudo-random numbers are generated from a "seed" number.
> | Starting from any given seed, the `random' function always generates
> | the same sequence of numbers.  Emacs always starts with the same seed
> | value, so the sequence of values of `random' is actually the same in
> | each Emacs run!  For example, in one operating system, the first call
> | to `(random)' after you start Emacs always returns -1457731, and the
> | second one always returns -7692030.  This repeatability is helpful for
> | debugging.
> | 
> |    If you want random numbers that don't always come out the same,
> | execute `(random t)'.  This chooses a new seed based on the current
> | time of day and on Emacs's process ID number.
> `----
>
>  When I evaluated (random t)
>
>    I got 80264346
>
>  But I need to get a single digit between 0-9 so I used (random 9)
>
>  But this then uses the same seed every time so the sequence of
>  numbers I'm returned will be repeated every time I restart Emacs.

Then you have not called again the seed function of random (random
t). I am using the elisp random function all the time to hear music
from my music library in a random fashion. And I do not get always the
same songs. They are always different.

Harald

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

* Re: How to create random characters?
       [not found]     ` <mailman.280.1071771962.868.help-gnu-emacs@gnu.org>
  2003-12-18 18:30       ` Harald Maier
@ 2003-12-18 18:31       ` Kai Grossjohann
  1 sibling, 0 replies; 9+ messages in thread
From: Kai Grossjohann @ 2003-12-18 18:31 UTC (permalink / raw)


Brad Collins <brad@studiojungle.net> writes:

>  When I evaluated (random t)
>
>    I got 80264346
>
>  But I need to get a single digit between 0-9 so I used (random 9)

I think you have a file which contains a function which invokes
random, right?

(defun brad-foo-func ()
  ...code which invokes (random 9)...)

My suggestion was to call (random t) *once*:

(random t)
(defun brad-foo-func ()
  ...code which invokes (random 9) ...)

I apologize if this was already clear, and if you tried it, and it
failed.  But I'm getting the impression you were trying something
else.

Kai

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

end of thread, other threads:[~2003-12-18 18:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-12-14  9:59 How to create random characters? Brad Collins
2003-12-14 11:10 ` Eli Zaretskii
     [not found] ` <mailman.53.1071403922.868.help-gnu-emacs@gnu.org>
2003-12-15  8:07   ` Joakim Hove
2003-12-15  8:46     ` Eli Zaretskii
2003-12-18  9:22 ` Brad Collins
     [not found] ` <mailman.242.1071742690.868.help-gnu-emacs@gnu.org>
2003-12-18 10:32   ` Kai Grossjohann
2003-12-18 17:30     ` Brad Collins
     [not found]     ` <mailman.280.1071771962.868.help-gnu-emacs@gnu.org>
2003-12-18 18:30       ` Harald Maier
2003-12-18 18:31       ` Kai Grossjohann

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