% ---------------------------------------------------------------------------- % % random.m % Ralph Becket <rbeck@microsoft.com> % Tue Jan 9 14:18:19 GMT 2001 % vim: ts=4 sw=4 et tw=0 wm=0 ff=unix % ---------------------------------------------------------------------------- % :- module mytest. :- interface. :- import_module io. :- pred main(io__state, io__state). :- mode main(di, uo) is det. :- implementation. :- import_module float, int, list, string, require. main --> io__command_line_arguments(ArgV), ( { ArgV = [], N = 1 } ; { ArgV = [Arg], N = string__det_to_int(Arg) } ; { ArgV = [_,_|_], error("usage: random [N]") } ), io__format("%.9f\n", [f(nth_random_no(N, seed))]). :- func nth_random_no(int, int) = float. nth_random_no(I, S0) = ( if I > 1 then nth_random_no(I - 1, S) else R ) :- gen_random(100.0, R, S0, S). :- pred gen_random(float, float, int, int). :- mode gen_random(in, out, in, out) is det. gen_random(Max, R, S0, S) :- S = (S0 * ia + ic) `rem` im, R = Max * float(S) / float(im). :- func im = int. im = 139968. :- func ia = int. ia = 3877. :- func ic = int. ic = 29573. :- func seed = int. seed = 42.