How to generate repeatable random sequences with rand-int

前端 未结 7 1586
你的背包
你的背包 2021-02-08 18:22

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

7条回答
  •  被撕碎了的回忆
    2021-02-08 18:43

    In the original question, two things are misunderstood:

    • How to use dynamic vars
    • How to use clojure.data.generators

    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
      )
    

提交回复
热议问题