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

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

Re: UUIDGEN in lisp


From: Jesper Harder
Subject: Re: UUIDGEN in lisp
Date: Sun, 15 Feb 2004 21:47:44 +0100
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

Brad Collins <brad@studiojungle.net> writes:

> The major mode I'm working on will be GPL'd, is it okay to include
> this code in the mode?

Sure.

> Interesting to see that if you call the script twice in less than a
> second you get a duplicate id....  not sure why, but it means that
> one should take care it in scripts....

Ah, yes.  It happens because of the `(random t)' in `uuid-random'.
It seeds the RNG, which is necessary because otherwise you'll always
get the same sequence of random number.

But Emacs uses the current time (plus pid) as the seed, so you'll get
the same numbers if you call it with intervals shorter than the time
resolution.  So you should remove the (random t) from the function:

(defun uuid-random ()
  "Return a list of 16 random bytes."
  (if (file-readable-p "/dev/urandom")
      (let ((coding-system-for-read 'binary))
        (mapcar 'identity
                (substring
                 (string-as-unibyte
                  (shell-command-to-string
                   "dd count=16 bs=1 < /dev/urandom"))
                 0 16)))
    (mapcar 'random (make-list 16 255))))

and just place it at the top-level (or in the function that starts
your mode).


reply via email to

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