On Dec 2, 10:42 am, bOR_ <[EMAIL PROTECTED]> wrote:
> I'll compare it later on to the one ruby is using (Mersenne Twister
> Algorithm, which is excellent for monte carlo-like simulations), just
> to get my own bearings.
>
An alternative to MT that is as good, according to RNG expert George
Marsaglia, but simpler and faster is Marsaglias multiply-with-carry
(MWC). There are of course plenty of MT implementations in Java out
there that you could reuse but if you need a pure clojure PRNG the
simplest MWC1 below might fit the bill.
I have not tested this extensively but I implemented his simplest MWC
PRNG (interface is not so nice maybe but I was playing with
multimethods and STMs ;)), FWIW:
(defstruct *mwc-rng* :rng-type :seed1 :seed2 :z :w :constz :constw)
(defn make-rng-mwc1 [seed1 seed2]
(struct *mwc-rng* :marsaglia-mwc1 seed1 seed2
(bit-and seed1 0xffffffff) (bit-and seed2 0xffffffff) 36969
18000))
(defmulti update :rng-type)
(defmulti randint32 :rng-type)
(defn mwc1 [c v]
(+ (* c (bit-and v 0xffff)) (bit-shift-right v 16)))
(defmethod update :marsaglia-mwc1
[rng]
(merge rng {:z (mwc1 (:constz rng) (:z rng))
:w (mwc1 (:constw rng) (:w rng))}))
(defmethod randint32 :marsaglia-mwc1
[rng]
(+ (bit-shift-left (bit-and (:z rng) 0xffff) 16)
(bit-and (:w rng) 0xffff)))
(defstruct *autoupdating-rng* :rng-type :rng-ref)
(defn make-rng-autoupdating [corerng]
(struct *autoupdating-rng* :autoupdating (ref corerng)))
(defn update-then-apply
[autorng fn]
(let [refrng (:rng-ref autorng)]
(dosync
(alter refrng #(update %))
(fn @refrng))))
(defmethod randint32 :autoupdating [rng]
(update-then-apply rng #(randint32 %)))
Easy to turn into a lazy stream version if preferred and type hints
could speed it up...
For details check: http://www.ms.uky.edu/~mai/RandomNumber
Cheers,
Robert Feldt
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---