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

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

Re: How to create random characters?


From: Brad Collins
Subject: Re: How to create random characters?
Date: Thu, 18 Dec 2003 16:22:03 +0700
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3 (windows-nt)

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





reply via email to

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