Emacs: Insert Random Number or String π
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
(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' Created: 2017-05-24 Version: 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)))))
Insert Random Hexadecimal
(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' Created: 2017-08-03 Version: 2024-07-17" (interactive (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 5))) (let ((xn (if CountX CountX 5))) (insert (format "%x" (random (read (concat "#x" (make-string xn ?f))))))))
Insert Random String
(defun xah-random-string (&optional CountX) "return a random string of length CountX. 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. URL `http://xahlee.info/emacs/emacs/elisp_insert_random_number_string.html' Version: 2024-04-03" (interactive ) (let ((xcharset "BCDFGHJKMNPQRSTVWXYZbcdfghjkmnpqrstvwxyz23456789") xcount xvec) (setq xcount (length xcharset)) (setq xvec (mapcar (lambda (_) (aref xcharset (random xcount))) (make-vector (if CountX CountX 5) 0))) (mapconcat 'char-to-string xvec))) (defun xah-insert-random-string (&optional CountX) "Insert a random string of length 5. If `universal-argument' is called first, ask for different length. See `xah-random-string' for detail. URL `http://xahlee.info/emacs/emacs/elisp_insert_random_number_string.html' Created: 2018-08-03 Version: 2024-04-02" (interactive (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 5))) (insert (xah-random-string CountX)))
O emacs! β₯
2010 Thanks to Teemu Likonen [tlikoβ¦@iki.fi] for improvement.