yazz.com
yazz.com

Reputation: 58796

How to get a random item from a list in Clojure?

If I have a list like:

["apples" "pears" "oranges"]

What is the the easiest way to get a random item from here?

Upvotes: 6

Views: 2106

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91577

rand-nth should do the trick:

user> (rand-nth ["apples" "pears" "oranges"])
"pears"
user> (rand-nth ["apples" "pears" "oranges"])
"oranges"
user> (rand-nth ["apples" "pears" "oranges"])
"oranges"

Upvotes: 15

Related Questions