agpl_1.0.0_b5da3320/src/agpl-random.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
with Ada.Numerics.Float_Random;

--  Shortcuts to some common random utilities.
--  This package if *not* thread safe.

--  During elaboration, the internal random generator is reset. This makes
--  each program run not repeatable.

package Agpl.Random is

   pragma Elaborate_Body;

   subtype Uniformly_Distributed is
     Ada.Numerics.Float_Random.Uniformly_Distributed;

   subtype Open_Uniformly_Distributed is
     Float range 0.0 .. Float'Pred (Uniformly_Distributed'Last);

   function Open_Uniform return Open_Uniformly_Distributed;
   --  Quick obtention of a random point in [0.0 .. 1.0)

   function Uniform return Uniformly_Distributed;
   --  Quick obtention of a random point in [0.0 .. 1.0]

   function Gaussian (Sigma : Float) return Float;
   --  Sigma is std dev

   function Get_Float (Min, Max : in Float) return Float;
   --  Get a float in [Min .. Max]

   generic
      type Discrete is (<>);
   function Uniform_Discrete return Discrete;
   --  Obtain a random value from a discrete type.

   function Get_Integer (Min, Max : in Integer) return Integer;
   --  Quick obtention of a random integer in [Min, Max]
   --  if Max < Min then Max is returned

   type Object is tagged limited private;
   --  Use this object to have repeteability.
   --  Unless reset, this object has a default initialization which will make
   --  all runs identic.

   function Open_Uniform (This : in Object) return Open_Uniformly_Distributed;

   procedure Reset (Initiator : in Integer);
   --  Reset the internal generator.

   procedure Reset (This : in out Object);
   --  This resets based in some clock value.

   procedure Reset (This : in out Object; Initiator : in Integer);
   --  This resets to a certain generator.

   function Uniform (This : in Object) return Uniformly_Distributed;

   function Gaussian (This : in Object; Sigma : Float) return Float;
   pragma Buggy ("There's no state per object, just one C side generator!" &
                 "So don't rely on separate objects, albeit you can replicate " &
                 "the sequence by seeding any of them...");
   --  Sigma is std dev

   function Get_Integer (This     : in Object;
                         Min, Max : in Integer) return Integer;
   --  if Max < Min then Max is returned

   function Get_Float (This     : in Object;
                       Min, Max : in Float) return Float;

   generic
      type Discrete is (<>);
   function Discrete_Random (This : in Object) return Discrete;

   function Flip_Coin return Boolean;
   --  50% chance of true

private

   type Object is tagged limited record
      Gen : Ada.Numerics.Float_Random.Generator;
   end record;

end Agpl.Random;