------------------------------------------------------------------------------
--                                                                          --
--                         GNAT RUNTIME COMPONENTS                          --
--                                                                          --
--         A D A . N U M E R I C S . D I S C R E T E _ R A N D O M          --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                            $Revision: 1.8 $                              --
--                                                                          --
--           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
--                                                                          --
-- The GNAT library is free software; you can redistribute it and/or modify --
-- it under terms of the GNU Library General Public License as published by --
-- the Free Software  Foundation; either version 2, or (at your option) any --
-- later version.  The GNAT library is distributed in the hope that it will --
-- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
-- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
-- Library  General  Public  License for  more  details.  You  should  have --
-- received  a copy of the GNU  Library  General Public License  along with --
-- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
-- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
--                                                                          --
------------------------------------------------------------------------------

with Calendar;
with Unchecked_Deallocation;

package body Ada.Numerics.Discrete_Random is

   --------------
   -- Finalize --
   --------------

   procedure Finalize (Gen : in out Generator) is

      procedure Destroy_State is
        new Unchecked_Deallocation (ANR.State, Access_State);

   begin
      Destroy_State (Gen.State);
   end Finalize;

   -----------
   -- Image --
   -----------

   function Image (Of_State : State) return String is
   begin
      return ANR.Image (ANR.State (Of_State));
   end Image;

   ------------
   -- Random --
   ------------

   function Random (Gen : Generator) return Result_Subtype is
      Lo, Hi : Float;
      F      : Float;
      R      : Float;
      U      : Float;

   begin
      ANR.Random (Gen.State.all, U);
      Lo := Float (Result_Subtype'Pos (Result_Subtype'First));
      Hi := Float (Result_Subtype'Pos (Result_Subtype'Last));
      R  := (Hi - Lo) * U - 0.5;

      return Result_Subtype'Val (Long_Long_Integer (R));
   end Random;

   -----------
   -- Reset --
   -----------

   procedure Reset (Gen : in Generator; Initiator : in Integer) is
   begin
      ANR.Reset (Gen.State.all, Initiator);
   end Reset;

   procedure Reset (Gen : in Generator) is
   begin
      ANR.Reset (Gen.State.all);
   end Reset;

   procedure Reset (Gen : in Generator; From_State : in State) is
   begin
      Gen.State.all := ANR.State (From_State);
   end Reset;

   ----------
   -- Save --
   ----------

   procedure Save (Gen : in Generator; To_State : out State) is
   begin
      To_State := State (Gen.State.all);
   end Save;

   -----------
   -- Value --
   -----------

   function Value (Coded_State : String) return State is
   begin
      return State (ANR.Value (Coded_State));
   end Value;

end Ada.Numerics.Discrete_Random;
