I want to be able to generate repeatable numbers using rand
in Clojure. (Specifically, I want results of calls to rand-nth
or Incanter\'s sample
In the original question, two things are misunderstood:
First, dynamic vars should be managed via the binding
macro. Second rand
is not a function of clojure.data.generators
, but of clojure.core
itself and thus resetting the *rnd*
var doesn't have effect. So here is how you should do it:
(require '[clojure.data.generators :as gen])
(binding [gen/*rnd* (java.util.Random. 437)]
(println (gen/double)) ;=> 0.7634858067742888
(println (gen/double)) ;=> 0.6959205688388975
)
(binding [gen/*rnd* (java.util.Random. 437)]
(println (gen/double)) ;=> 0.7634858067742888
(println (gen/double)) ;=> 0.6959205688388975
)