On Aug 7, 6:17 pm, Sean Devlin <francoisdev...@gmail.com> wrote: > Ok, I need some help. I'm writing some tests for c.c.seq-utils, and I > ran into a problem defining a test for both shuffle and rand-elt. > > Does anyone here have any experience writing tests for random > functions? Am I going to need to use serious statistics the answer > this? > > Ideas?
It depends upon what you want to test. If you want to test that rand- elt selects an item from the seq s independently on each run, and with equal probability for all elements, then that is effectively like testing rand-int for the same properties, and yes, you will have to get into some statistics to do so. However, if you back off from testing the random part of the function, there are some "invariants" that are always true that you could test, such as: (1) the value x returned by rand-elt should always be a member of the seq s (2) the value returned by shuffle should always have the same number of each item as the input seq s. That is, if you evaluate the function 'tally' below on the input seq s and on the returned seq t, you should always find (= (tally s) (tally t)) is true. (defn tally [s] (loop [s s t (transient {})] (if-let [s (seq s)] (let [item (first s)] (recur (next s) (assoc! t item (inc (get t item 0))))) (persistent! t)))) Andy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---