random-sample

added
1.7

ns
clojure.core

type
function

(random-sample prob) (random-sample prob coll)

Returns items from coll with random probability of prob (0.0 -
1.0).  Returns a transducer when no collection is provided.

                ;; The output of random-sample is a sequence.
;; Each element of the original collection has probability "prob"
;; of being included in the output sequence.

(random-sample 0.5 [1 2 3 4 5])
;;=> (1 2 4)


;; random-sample can operate on an infinite sequence,
;; producing an infinite sequence.

(take 10 (random-sample 0.1 (repeat :foo)))
;;=> (:foo :foo :foo :foo :foo :foo :foo :foo :foo :foo)

(take 10 (random-sample 0.01 (range)))
;;=> (57 113 281 286 352 497 727 768 957 960)