Emacs: Insert Random Number or String π
This page shows emacs lisp commands to insert random {number, string, hexadecimal}.
Set Random Seed
(random t) ; seed random number
note, starting in Emacs 24 (Released 2012-06), emacs sets a random seed when emacs starts. Before, random sequence are always the same if you don't set seed.
Insert Random Number, Hexadecimal, String
(defun xah-insert-random-number (&optional CountX) "Insert CountX random digits. CountX default to 3. If `universal-argument' is called first, ask for count of digits. URL `http://xahlee.info/emacs/emacs/elisp_insert_random_number_string.html' Version: 2017-05-24 2024-03-30 2024-04-02" (interactive (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 3))) (let ((xn (if CountX CountX 3))) (insert (mapconcat (lambda (_) (number-to-string (random 10))) (make-list xn 0))))) (defun xah-insert-random-hex (&optional CountX) "Insert CountX random hexadecimal digits. CountX default to 5. Call `universal-argument' and type a number, then, call this command, for different count. URL `http://xahlee.info/emacs/emacs/elisp_insert_random_number_string.html' Version: 2017-08-03 2024-03-30 2024-04-02" (interactive (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 5))) (let ((xn (if CountX CountX 5))) (insert (format (concat "%0" (number-to-string xn) "x") (random (expt 16 xn)))))) (defun xah-insert-random-string (&optional CountX) "Insert a random string of length 5. The possible chars are: 2 to 9, upcase or lowercase English alphabet but no a e i o u, no L l and no 0 1. If `universal-argument' is called first, ask for different length. URL `http://xahlee.info/emacs/emacs/elisp_insert_random_number_string.html' Version: 2018-08-03 2024-03-30 2024-04-02" (interactive (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 5))) (let ((xcharset "BCDFGHJKMNPQRSTVWXYZbcdfghjkmnpqrstvwxyz23456789") xcount xvec) (setq xcount (length xcharset)) (setq xvec (mapcar (lambda (_) (aref xcharset (random xcount))) (make-vector (if CountX CountX 5) 0))) (insert (mapconcat 'char-to-string xvec))))
O emacs! β₯
2010 Thanks to Teemu Likonen [tlikoβ¦@iki.fi] for improvement.