@end example
@end defmac
+@defun seq-random-elt sequence
+ This function returns an element of @var{sequence} taken at random. If @var{sequence} is nil, the function returns nil.
+
+@example
+@group
+(seq-random-elt [1 2 3 4])
+@result{} 3
+(seq-random-elt [1 2 3 4])
+@result{} 2
+(seq-random-elt [1 2 3 4])
+@result{} 4
+(seq-random-elt [1 2 3 4])
+@result{} 2
+(seq-random-elt [1 2 3 4])
+@result{} 1
+@end group
+@end example
+
+ If @var{sequence} is empty, the function signals an error.
+@end defun
@node Arrays
@section Arrays
;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: sequences
-;; Version: 2.18
+;; Version: 2.19
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
"Return element of SEQUENCE at the index N.
If no element is found, return nil."
(ignore-errors (seq-elt sequence n)))
+
+(cl-defgeneric seq-random-elt (sequence)
+ "Return a random element from SEQUENCE.
+Return nil if SEQUENCE is nil."
+ (if (seq-empty-p sequence)
+ (error "Sequence cannot be empty")
+ (seq-elt sequence (random (seq-length sequence)))))
\f
;;; Optimized implementations for lists
(require 'ert)
(require 'seq)
+(require 'map)
(defmacro with-test-sequences (spec &rest body)
"Successively bind VAR to a list, vector, and string built from SEQ.
(should (equal (seq-sort-by #'seq-length #'> seq)
["xxx" "xx" "x"]))))
+(ert-deftest test-seq-random-elt-take-all ()
+ (let ((seq '(a b c d e))
+ (count '()))
+ (should (= 0 (map-length count)))
+ (dotimes (_ 1000)
+ (let ((random-elt (seq-random-elt seq)))
+ (map-put count
+ random-elt
+ (map-elt count random-elt 0))))
+ (should (= 5 (map-length count)))))
+
+(ert-deftest test-seq-random-elt-signal-on-empty ()
+ (should-error (seq-random-elt nil))
+ (should-error (seq-random-elt []))
+ (should-error (seq-random-elt "")))
+
(provide 'seq-tests)
;;; seq-tests.el ends here