Skip to content

Instantly share code, notes, and snippets.

@drewr
Created May 20, 2010 19:37
Show Gist options
  • Select an option

  • Save drewr/407988 to your computer and use it in GitHub Desktop.

Select an option

Save drewr/407988 to your computer and use it in GitHub Desktop.
(ns foo.randomizer
(:use [clojure.test]))
(defn delete [coll idx]
(if (<= (inc idx) (count coll))
(let [coll (vec coll)]
(concat (subvec coll 0 idx) (subvec coll (inc idx) (count coll))))
coll))
(defn randomize [coll]
(loop [from coll to []]
(if (seq from)
(let [idx (rand-int (count from))]
(recur (delete from idx) (conj to (nth (vec from) idx))))
to)))
(deftest test-rand
(testing "should return the same number of elements"
(is (= 3 (count (randomize [1 2 3])))))
(testing "should not be the same vector"
(let [v (range 100)
vprime (randomize v)]
(is (not= v vprime))
(is (apply = (map #(into #{} %) [v vprime]))))))
(comment
(randomize ['tim 'phil 'kevin 'jim 'drew 'dan 'steve 'paul])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment