UNIT math_random; {-*-Mode: fundamental-mode}

{-------- Random Number Generator -------------------}
INTERFACE
function  random_boolean                                 : boolean;
function  random_integer  (minimum, maximum : integer)   : integer;
function  random_shortreal(minimum, maximum : shortreal) : shortreal;
function  random_double   (minimum, maximum : double)    : double;



{-------- Random Number Generator -------------------}
IMPLEMENTATION
const offset = 0.211327;
      factor = 9821.0;

var   rn_boolean, rn_integer, rn_real, rn_double: double := 0.0;     {random generator}

procedure random_init (init_boolean, init_integer, init_real, init_double: double);
procedure reduce (init: double; var rn: double);
begin if init >= 0.0 then rn:= init - trunc (init) end;
begin
 reduce (init_boolean, rn_boolean);
 reduce (init_integer, rn_integer);
 reduce (init_real,    rn_real);
 reduce (init_double,  rn_double)
end;

function random_boolean: boolean; 
begin
  rn_boolean      := offset + rn_boolean * factor;
  rn_boolean      := rn_boolean - trunc (rn_boolean);
  random_boolean  := rn_boolean > 0.5
end;

function random_integer    (minimum, maximum: integer): integer; 
begin
  rn_integer      := offset + rn_integer * factor;
  rn_integer      := rn_integer - trunc (rn_integer);
  random_integer  := minimum + trunc (rn_integer * (maximum - minimum + 1))
end;

function  random_shortreal (minimum, maximum : shortreal): shortreal;
begin 
  rn_real         := offset + rn_real * factor;
  rn_real         := rn_real - trunc (rn_real);
  random_shortreal:= rn_real * (maximum - minimum) + minimum
end;

function random_double     (minimum, maximum : double): double;
begin 
  rn_double      := offset + rn_double * factor;
  rn_double      := rn_double - trunc (rn_double);
  random_double  := rn_double * (maximum - minimum) + minimum
end;

BEGIN
  random_init (0.123456, 0.78901234, 0.5678901234, 0.5678901234)
END.
