range vs sample semantics
Closed this issue · 2 comments
Really nice work!!
One thing that comes in mind:
In the example provided in the readme, there seems to be a variation in semantics whether we're dealing with Random number generation or Sampling.
;random
(def rng (random/create "foobar"))
(random/next-int! rng 5)
;sample
(sample (range 5) :seed "foobar")
Would it be interesting to have consistent semantics?
(random/next-int! (range 5) :seed "foobar")
(sample (range 5) :seed "foobar")
Congrats for your excellent work! Looking forward for using that lib!
Thanks!
But providing a seed to random/next-int!
rather than a mutable generator doesn't make much sense to me. For a given seed we'd always end up generating the same number. Instead random/next-int
could return both a random number and a new seed for the next call, but using that sounds awkward to me.
A seed could be nice, however, if it were used to generate an entire sequence of random numbers:
(defn rand-ints
"Generates an infinite seq of integers from [minimum, maximum)."
[minimum maximum & {:keys [seed]}]
(let [rng (random/create (or seed (rand)))
diff (- maximum minimum)]
(repeatedly #(+ minimum (random/next-int! rng diff)))))
Is that closer to what you were thinking?
oh, your're right!
rand-ints
is really nice!