Emacs: Insert Random Number/Hex/String

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 4.
Call `universal-argument' before for different count.
URL `http://ergoemacs.org/emacs/elisp_insert_random_number_string.html'
Version 2017-05-21"
  (interactive "P")
  (let ((ξcharset "1234567890" )
        (ξbaseCount 10))
    (dotimes (_ (if (numberp φn) (abs φn) 4 ))
      (insert (elt ξcharset (random ξbaseCount))))))

Insert Random Hexadecimal

(defun xah-insert-random-hex (φn)
  "Insert φn random hexadecimal digits.
φn default to 4.
Call `universal-argument' before for different count.
URL `http://ergoemacs.org/emacs/elisp_insert_random_number_string.html'
Version 2017-05-21"
  (interactive "P")
  (let* ((ξcharset "0123456789abcdef" )
         (ξbaseCount (length ξcharset)))
    (dotimes (_ (if (numberp φn) (abs φn) 4 ))
      (insert (elt ξcharset (random ξbaseCount))))))

Insert Random String

(defun xah-insert-random-string (φn)
  "Insert a random alphanumerics string of length 4.
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 2017-05-21"
  (interactive "P")
  (let* ((ξcharset "1234567890abcdefghijklmnopqrstuvwxyz")
         (ξbaseCount (length ξcharset)))
    (dotimes (_ (if (numberp φn) (abs φn) 4))
      (insert (elt ξcharset (random ξbaseCount))))))

Insert Random UUID

Elisp: Insert Random UUID

O emacs! ♥

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

Like it? Buy Xah Emacs Tutorial. Thanks.