-- $Id: random.gnat,v 1.0 2003/06/11 12:05:00 dada Exp $
-- http://dada.perl.it/shootout/
-- Ada 95 code by C.C.
with System, Ada.Command_Line, Ada.Text_IO;
procedure Random is
type Real is digits Positive'Max (15, System.Max_Digits);
package Rio is new Ada.Text_IO.Float_IO (Num => Real);
package Random_Real is
function Gen_Random (Supr : Real) return Real;
pragma Inline (Gen_Random);
end Random_Real;
package body Random_Real is
IM : constant Positive := 139968;
IA : constant Integer := 3877;
IC : constant Integer := 29573;
Last : Integer := 42;
function Gen_Random (Supr : Real) return Real is
pragma Suppress (Overflow_Check);
pragma Suppress (Range_Check);
begin
Last := (Last * IA + IC) mod IM;
return Supr * Real (Last) / Real (IM);
end Gen_Random;
-- Assume no overflow for "Natural ((IM - 1) * IA + IC)"
end Random_Real;
Result : Real;
N : Natural := 0;
begin
begin
N := Natural'Value (Ada.Command_Line.Argument (1));
exception
when Constraint_Error => null;
end;
for Iter in 1 .. N loop
Result := Random_Real.Gen_Random (Supr => 100.0);
end loop;
Rio.Put (Result, Fore => 0, Aft => 9, Exp => 0);
Ada.Text_IO.New_Line;
end Random;