Clojure's rand
and rand-int
use java.util.Random
as the underlying random number generator. If your application depends heavily on random numbers, you might want to consider using a higher-quality random number generator written in Java, such as MersenneTwisterFast. This has a nextLong()
method, and it's very easy to use from Clojure. Java's standard class SecureRandom
might be worth considering, too; it's designed for different purposes than the Mersenne Twister. There are other good Java random number generators available. Depends on what you're using the random numbers for. For occasional use of random numbers, java.util.Random
might be just fine. There are additional options mentioned in comments by others.
I'll describe use of MersenneTwisterFast
. Using the other classes I mentioned would be essentially the same, but without the initial steps.
With Leiningen, add something like this to project.clj:
:java-source-paths ["src/java"]
and then put the Java source for MersenneTwisterFast.java in src/java/ec/util. Then you can do this:
(ns my.namespace
(:import [ec.util MersenneTwisterFast]))
(def rng (MersenneTwisterFast. 42)) ; Specify a different seed, e.g. from system time.
(defn next-long [] (.nextLong rng))