如何从Clojure中的列表中获取随机项?

时间:2014-06-03 20:58:10

标签: clojure

如果我有一个列表:

["apples" "pears" "oranges"]

从这里获取随机物品的最简单方法是什么?

1 个答案:

答案 0 :(得分:10)

rand-nth应该这样做:

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