philipbohun.com

Glojure by Example: Random Numbers

The top-level math/rand functions are seeded automatically, so two runs do not match. NewSource and New build a generator with a fixed seed. Intn returns an integer in [0, n). Float64 returns a float in [0.0, 1.0).

(def rng (math:rand.New (math:rand.NewSource 1)))
(println (.Intn rng 100))
(println (.Intn rng 100))
(let [x (.Float64 rng)]
  (println (and (>= x 0) (< x 1))))

Keep the generator and draw from it. A new NewSource with the same seed repeats the sequence.

A generator seeded with 1 repeats these draws.

$ glj random-numbers.glj
81
87
true

Next example: Number Parsing.