Emacs: Insert Random Number or String π
Set Random Seed
(random t) ; seed random number
Starting in Emacs 24 (date 2012), 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 4. Padded by 0. 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: 2025-04-21" (interactive (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 4))) (let ((xn (if CountX CountX 4))) (insert (format (concat "%0" (number-to-string xn) "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. First char is always a letter URL `http://xahlee.info/emacs/emacs/elisp_insert_random_number_string.html' Created: 2024-04-03 Version: 2025-04-18" (interactive) (let (xletters xcharset xlen xtail) (setq xletters "BCDFGHJKMNPQRSTVWXYZbcdfghjkmnpqrstvwxyz") (setq xcharset (concat xletters "23456789")) (setq xlen (length xcharset)) (setq xtail (mapcar (lambda (_) (aref xcharset (random xlen))) (make-list (if CountX (1- CountX) (1- 5)) 0))) (concat (list (aref xletters (random (length xletters)))) xtail)))
(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.
Insert Random Chinese Chars
(defun xah-insert-random-chinese-char (&optional Ncount) "Insert Ncount random chinese character. Ncount default to 5. If `universal-argument' is called first, ask for count of digits. URL `http://xahlee.info/emacs/emacs/elisp_insert_random_number_string.html' Created: 2025-05-05 Version: 2025-05-05" (interactive (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 5))) (let ((cidstart #x4E00) (cidend #x9FFF) xresult) ;; range of chinese chars in unicode, #x4E00 to #x9FFF (dotimes (_ (if Ncount Ncount 5)) (push (+ cidstart (random (1+ (- cidend cidstart)))) xresult)) (insert (concat xresult))))