ClojureDocs

导航

命名空间

rand-nth

clojure.core

1.2 版本及更新版本可用 (源代码)
  • (rand-nth coll)
Return a random element of the (sequential) collection. Will have
the same performance characteristics as nth for the given
collection.
3 Examples
user=> (def food [:ice-cream :steak :apple])
#'user/food

user=> (rand-nth food)
:apple
user=> (rand-nth food)
:ice-cream
user=> (def food [:ice-cream :steak :apple])
#'user/food

user=> (rand-nth food)
:ice-cream
user=> (rand-nth food)
:ice-cream
user=> (rand-nth [])
IndexOutOfBoundsException   clojure.lang.PersistentVector.arrayFor (PersistentVector.java:158)

user=>
(let [xs []]
  (when (not (empty? xs))
    (rand-nth xs)))
nil
See Also

Returns a random floating point number between 0 (inclusive) and n (default 1) (exclusive).

Added by alimoeeny

Return a random permutation of coll

Added by timgilbert

Returns the value at the index. get returns nil if index out of bounds, nth throws an exception un...

Added by green-coder
0 Notes
No notes for rand-nth