You're viewing version 1.2.0 of rand-nth. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

rand-nth

clojure.core

  • (rand-nth coll)
Return a random element of the (sequential) collection. Will have
the same performance characteristics as nth for the given
collection.

1 Example top

  • user=> (def food [:ice-cream :steak :apple])
    #'user/food
    
    user=> (rand-nth food)
    :apple
    user=> (rand-nth food)
    :ice-cream
    
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/rand

    Returns a random floating point number between 0 (inclusive) and n

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5591 top

(defn rand-nth
  "Return a random element of the (sequential) collection. Will have
  the same performance characteristics as nth for the given
  collection."
  {:added "1.2"}
  [coll]
  (nth coll (rand-int (count coll))))
Vars in clojure.core/rand-nth: count defn nth rand-int

Comments top

No comments for rand-nth. Log in to add a comment.