--  Ada.Numerics.Discrete_Random (RM A.5.2): uniformly distributed values of a discrete
--  subtype from a generator the program declares and passes. A generator's state is an object
--  of its own, changed by each call (Random is a function with side effects on Gen), so no
--  state is hidden from the proof; a result is known only to lie in its range. The generator is
--  xoshiro256** (Blackman and Vigna, 2018), seeded through SplitMix64: a state of four 64-bit
--  words, period 2 ** 256 - 1. It is not for cryptography.
with Interfaces;

generic
   type Result_Subtype is (<>);
package Ada.Numerics.Discrete_Random is
   type Generator is limited private;

   --  the next value: the next 64-bit word modulo the subtype's number of values (so each
   --  value comes with a probability within Count / 2 ** 64 of 1 / Count)
   function Random (Gen : in out Generator) return Result_Subtype
     with Side_Effects, Global => null,
          Pre => Result_Subtype'First <= Result_Subtype'Last;

   --  the next value from First .. Last, likewise
   function Random (Gen : in out Generator; First, Last : Result_Subtype) return Result_Subtype
     with Side_Effects, Global => null,
          Pre => First <= Last,
          Post => Random'Result in First .. Last;

   --  the state the initiator chooses: the same initiator gives the same values
   procedure Reset (Gen : in out Generator; Initiator : Integer)
     with Global => null;

   --  a state chosen by the time (the monotonic clock's nanoseconds)
   procedure Reset (Gen : in out Generator)
     with Global => null;

   type State is private;

   --  the generator's state, to Reset it to later
   procedure Save (Gen : Generator; To_State : out State)
     with Global => null;

   --  the state a Save gave
   procedure Reset (Gen : in out Generator; From_State : State)
     with Global => null;

   --  a state's image: its four words in hexadecimal, 16 digits each
   Max_Image_Width : constant := 64;

   --  (Verada) whether a string is the image of a state: Max_Image_Width hexadecimal digits,
   --  of either case
   function Is_Image (Coded_State : String) return Boolean is
     (Coded_State'Length = Max_Image_Width
      and then (for all C of Coded_State => C in '0' .. '9' | 'A' .. 'F' | 'a' .. 'f'));

   function Image (Of_State : State) return String
     with Global => null,
          Post => Image'Result'First = 1 and then Is_Image (Image'Result);

   --  the state an image names (where the RM raises Constraint_Error, the precondition
   --  requires an image; the image of no state, all zeros, gives Reset (Gen, 0)'s state)
   function Value (Coded_State : String) return State
     with Global => null,
          Pre => Is_Image (Coded_State);
private
   use type Interfaces.Unsigned_64;

   type Words is array (1 .. 4) of Interfaces.Unsigned_64;

   type State is record
      S : Words := (16#E220A8397B1DCDAF#, 16#6E789E6AA1B965F4#, 16#06C45D188009454F#, 16#F88BB8A8724C81EC#);
   end record;

   type Generator is limited record
      Current : State;
   end record;
end Ada.Numerics.Discrete_Random;
