%%% -*- mode: erlang -*-
%%% $Id: random.erlang,v 1.3 2001/05/18 07:39:45 doug Exp $
%%% http://www.bagley.org/~doug/shootout/

-module(random).
-export([main/1]).

main() -> main(['1']).
main([Arg]) ->
    N = list_to_integer(atom_to_list(Arg)),
    io:fwrite("~.9f\n", [rand(N, 42, 0.0, 100.0)]),
    halt(0).


-define(IM, 139968).
-define(IA, 3877).
-define(IC, 29573).


rand(0, _, Rand, _) -> Rand;
rand(N, Seed, Rand, Max) ->
    NewSeed = (Seed * ?IA + ?IC) rem ?IM,
    NewRand = Max * NewSeed / ?IM,
    rand(N-1, NewSeed, NewRand, Max).