12

I have some functions that require a series of random numbers so I've taken some simple primitives such as #(inc (g/uniform 0 n)) and I can't seem to generate a reproducible series of random numbers, even though I'm rebinding *rnd*, unless I generate them as shown below. I can't imagine that's the best way, so can anyone point out how to do it better?

Note: I run each example below three times as shown to produce the given results.

(ns example.show
  (:require [clojure.data.generators :as g]))


(binding [g/*rnd* (java.util.Random. 42)]
  (take 10 (repeatedly #(inc (g/uniform 0 n))))

=> (9 4 5 4 4 5 1 8 2 9)

=> (2 1 1 6 3 10 10 4 1 9)

=> (10 4 7 8 9 6 10 1 8 3)


(binding [g/*rnd* (java.util.Random. 42)]
  (g/reps 10 #(inc (g/uniform 0 n)))

=> (3 9 4 6 3 8 6 6 5 4)

=> (7 8 4 7 7 5 7 4 8 7)

=> (2 8 7 8 8 8 9 2 6 5)

;; This seems to work
(binding [g/*rnd* (java.util.Random. 42)]
  (letfn [(roll [n] #(inc (g/uniform 0 n)))]
    [((roll 10)) ((roll 10)) ((roll 10)) ((roll 10)) ((roll 10)) ((roll 10)) ((roll 10)) ((roll 10)) ((roll 10)) ((roll 10))]))

=> [8 7 4 3 7 10 4 3 5 8]

=> [8 7 4 3 7 10 4 3 5 8]

=> [8 7 4 3 7 10 4 3 5 8]

2 Answers 2

11

Because of laziness. You return from binding before the sequence is realized. Therefore, the lazy sequence never sees the bindings you set. One solution would be to force realization with a doall on the sequence inside the binding.

2
  • I thought lazy evaluation just deferred execution, perhaps by wrapping parameters in a closure, does this affect any other order specific consuming function? How do I handle it without doall as my computation could be expensive and I don't want to just throw away laziness. Commented Mar 17, 2014 at 10:58
  • 1
    See kotka.de/blog/2009/11/Taming_the_Bound_Seq.html for explanation and workaround.
    – A. Webb
    Commented Mar 17, 2014 at 17:04
5

If, as your comment on the other answer indicates, you want to preserve the laziness, then you would need to apply the binding in the function passed to repeatedly, capturing a seed that you created outside of the lazy seq. For instance:

(defn rand-seq [seed]
  (let [r (java.util.Random. seed)]
    (repeatedly #(binding [g/*rnd* r]
                   (inc (g/uniform 0 10))))))

(take 10 (rand-seq 42))
#=> (8 7 4 3 7 10 4 3 5 8)

(take 10 (rand-seq 42))
#=> (8 7 4 3 7 10 4 3 5 8)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.