[Shootout-list] Improvements to ocaml Random entry
Yaron Minsky
yminsky@cs.cornell.edu
Thu, 17 Jun 2004 22:07:02 -0400
I've got a somewhat improved version of the ocaml random number
generator. it's a bit longer in lines, but only because it's in more
idiomatic ocaml form. For some reason, the ocaml entries all seem
artificially low on carriage returns.
Anyway, this is more straightforward and about 15-20% faster (at
least, when the RNG function is run in a tight loop. I'd think
everything would get dwarfed by the printf in the shootout.)
let im = 139968
and ia = 3877
and ic = 29573
and first_last = 42
let n = try int_of_string Sys.argv.(1) with _ -> 1
let rec gen_random max last i =
let new_last = (last * ia + ic) mod im in
if i > 1 then gen_random max new_last (i - 1)
else max *. float_of_int new_last /. float im
let () = Printf.printf "%9f\n" (gen_random 100.0 first_last n)