(* -*- mode: sml -*-
 * $Id: random.smlnj,v 1.5 2001/07/09 00:25:28 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *)

structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

val im : int = 139968;
val ia : int = 3877;
val ic : int = 29573;

fun randloop 0 seed rand max = rand
  | randloop n seed rand max = 
    let
    val newseed : int = (seed * ia + ic) mod im;
    val newrand : real = max * (Real.fromInt newseed) / (Real.fromInt im);
    in
        randloop (n-1) newseed newrand max
    end;


fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;

fun main(name, args) = 
  let
    val arg = hd(args @ ["1"]);
    val num = atoi arg;
    val result = randloop num 42 0.0 100.0;
  in
      print (Real.fmt (StringCvt.FIX (SOME 9)) result); print "\n";
      OS.Process.success
  end
end

val _ = SMLofNJ.exportFn("random", Test.main);