* lisp/emacs-lisp/seq.el (seq-random-elt): Autoload.
* lisp/avoid.el (mouse-avoidance-random-shape):
* lisp/epa-ks.el (epa-ks--query-url):
* lisp/erc/erc-networks.el (erc-server-select):
* lisp/gnus/gnus-fun.el (gnus--random-face-with-type)
(gnus-fun-ppm-change-string):
* lisp/net/soap-inspect.el (soap-sample-value-for-xs-simple-type):
* lisp/obsolete/landmark.el (landmark-random-move):
* lisp/play/mpuz.el (mpuz-build-random-perm):
* lisp/play/zone.el (zone-pgm-stress):
* lisp/vc/add-log.el (add-change-log-entry):
* test/lisp/net/tramp-tests.el
(tramp-test44-asynchronous-requests): Prefer seq-random-elt to
nth+random.
;;
;; (if (eq window-system 'x)
;; (mouse-avoidance-set-pointer-shape
-;; (nth (random 4)
+;; (seq-random-elt
;; (list x-pointer-man x-pointer-spider
;; x-pointer-gobbler x-pointer-gumby))))
;;
;; Internal variables
(defvar mouse-avoidance-state nil)
(defvar mouse-avoidance-pointer-shapes nil)
-(defvar mouse-avoidance-n-pointer-shapes 0)
(defvar mouse-avoidance-old-pointer-shape nil)
(defvar mouse-avoidance-animating-pointer nil)
(all-completions "x-pointer-" obarray
(lambda (x)
(and (boundp x)
- (integerp (symbol-value x)))))))
- (setq mouse-avoidance-n-pointer-shapes
- (length mouse-avoidance-pointer-shapes))))
- (nth (random mouse-avoidance-n-pointer-shapes)
- mouse-avoidance-pointer-shapes))
+ (integerp (symbol-value x)))))))))
+ (seq-random-elt mouse-avoidance-pointer-shapes))
(defun mouse-avoidance-ignore-p ()
(let ((mp (mouse-position)))
If no element is found, return nil."
(ignore-errors (seq-elt sequence n)))
+;;;###autoload
(cl-defgeneric seq-random-elt (sequence)
"Return a random element from SEQUENCE.
Signal an error if SEQUENCE is empty."
(cond ((null epa-keyserver)
(user-error "Empty keyserver pool"))
((listp epa-keyserver)
- (nth (random (length epa-keyserver))
- epa-keyserver))
+ (seq-random-elt epa-keyserver))
((stringp epa-keyserver)
epa-keyserver)
((error "Invalid type for `epa-keyserver'")))
(ports (if (listp (nth 3 srv))
(erc-ports-list (nth 3 srv))
(list (nth 3 srv))))
- (port (nth (random (length ports)) ports)))
+ (port (and ports (seq-random-elt ports))))
(erc :server host :port port)))
;;; The following experimental
(remove nil (mapcar
(lambda (f) (unless (string-match (or omit "^$") f) f))
(directory-files dir t ext))))
- (file (nth (random (length files)) files)))
+ (file (and files (seq-random-elt files))))
(when file
(funcall fun file)))))
(let* ((possibilities '("%02x0000" "00%02x00" "0000%02x"
"%02x%02x00" "00%02x%02x" "%02x00%02x"))
(format (concat "'#%02x%02x%02x' '#"
- (nth (random 6) possibilities)
+ (seq-random-elt possibilities)
"'"))
(values nil))
(dotimes (i 255)
(cond
((soap-xs-simple-type-enumeration type)
(let ((enumeration (soap-xs-simple-type-enumeration type)))
- (nth (random (length enumeration)) enumeration)))
+ (and enumeration (seq-random-elt enumeration))))
((soap-xs-simple-type-pattern type)
(format "a string matching %s" (soap-xs-simple-type-pattern type)))
((soap-xs-simple-type-length-range type)
(t (random 100)))))
((consp (soap-xs-simple-type-base type)) ; an union of values
(let ((base (soap-xs-simple-type-base type)))
- (soap-sample-value (nth (random (length base)) base))))
+ (soap-sample-value (and base (seq-random-elt base)))))
((soap-xs-basic-type-p (soap-xs-simple-type-base type))
(soap-sample-value (soap-xs-simple-type-base type))))))
(mapc
(lambda (direction) (put direction 'y_t 0))
landmark-directions)
- (dolist (direction (nth (random 8) landmark-8-directions))
+ (dolist (direction (seq-random-elt landmark-8-directions))
(put direction 'y_t 1.0))
(landmark-move))
(index 10)
elem)
(while letters
- (setq elem (nth (random index) letters)
+ (setq elem (seq-random-elt letters)
letters (delq elem letters)
index (1- index))
(aset mpuz-digit-to-letter index elem)
(forward-line -1)
(delete-region (point) (line-beginning-position 2))
(goto-char (point-min))
- (insert (nth (random (length lines)) lines)))
+ (insert (seq-random-elt lines)))
(message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
(sit-for 0.1)))))
(not (looking-at "[ \t]+.*<.*>$")))
(setq hit t)))))
(forward-line 1)
- (insert (nth (random (length new-entries))
- new-entries)
+ (insert (and new-entries (seq-random-elt new-entries))
(if use-hard-newlines hard-newline "\n")
(if use-hard-newlines hard-newline "\n"))
(forward-line -1))))
(let ((time (float-time))
(default-directory tmp-name)
(file
- (buffer-name
- (nth (random (length buffers)) buffers)))
+ (buffer-name (seq-random-elt buffers)))
;; A remote operation in a timer could
;; confuse Tramp heavily. So we ignore this
;; error here.
;; the buffers. Mix with regular operation.
(let ((buffers (copy-sequence buffers)))
(while buffers
- (let* ((buf (nth (random (length buffers)) buffers))
+ (let* ((buf (seq-random-elt buffers))
(proc (get-buffer-process buf))
(file (process-get proc 'foo))
(count (process-get proc 'bar)))