Emacs Lisp: Insert Random Number / Hexadecimal

By Xah Lee. Date: . Last updated: .

This page shows emacs lisp commands to insert random {number, string, hexadecimal}.

First, in your emacs init file, you should change the random seed, like this:

(random t) ; seed random number

Else, every emacs restart, the sequence of random will be the same.

Insert Random Number

(defun xah-insert-random-number (*n)
  "Insert *n random digits.
*n default to 5.
Call `universal-argument' before for different count.
URL `http://ergoemacs.org/emacs/elisp_insert_random_number_string.html'
Version 2016-01-12"
  (interactive "P")
  (let ((-charset "1234567890" )
        (-baseCount 10))    
    (dotimes (-i (if (numberp *n) (abs *n) 5 ))
      (insert (elt -charset (random -baseCount))))))

Insert Random Hexadecimal

(defun xah-insert-random-hex (*n)
  "Insert *n random hexidecimal digits.
*n default to 8.
Call `universal-argument' before for different count.
URL `http://ergoemacs.org/emacs/elisp_insert_random_number_string.html'
Version 2016-01-12"
  (interactive "P")
  (let* ((-charset "0123456789abcdef" )
         (-baseCount (length -charset)))
    (dotimes (-i (if (numberp *n) (abs *n) 8 ))
      (insert (elt -charset (random -baseCount))))))

Insert Random String

(defun xah-insert-random-string (*n)
  "Insert a random alphanumerics string of length 10.
The possible chars are 0 to 9, and a to z (lower case).
Call `universal-argument' before for different count.
URL `http://ergoemacs.org/emacs/elisp_insert_random_number_string.html'
Version 2016-01-12"
  (interactive "P")
  (let* ((-charset "1234567890abcdefghijklmnopqrstuvwxyz")
         (-baseCount (length -charset)))
    (dotimes (-i (if (numberp *n) (abs *n) 10))
      (insert (elt -charset (random -baseCount))))))

Insert Random UUID

Emacs Lisp: Insert Random UUID

O emacs! ♥

Thanks to Teemu Likonen 〔tliko…@iki.fi〕 for improvement.

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs