How to generate repeatable random sequences with rand-int

前端 未结 7 1587
你的背包
你的背包 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:47

    Probably not the cleanest way, but you can make it work by redefining clojure.core/rand:

    (ns clojure.core)
    
    (def r (java.util.Random. 1))
    
    (defn rand
      ([] (.nextDouble r))
      ([n] (.nextInt r n)))
    
    (take 10 (repeatedly #(rand-int 10)))
    

    This produces (5 8 7 3 4 4 4 6 8 8) every time I run it.

提交回复
热议问题