OSDN Git Service

2008-04-08 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-rannum.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                   G N A T . R A N D O M _ N U M B E R S                  --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2007, Free Software Foundation, Inc.              --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Extended pseudo-random number generation
35
36 --  This package provides a type representing pseudo-random number generators,
37 --  and subprograms to extract various distributions of numbers from them. It
38 --  also provides types for representing initialization values and snapshots of
39 --  internal generator state, which permit reproducible pseudo-random streams.
40
41 --  The generator currently provided by this package has an extremely long
42 --  period (at least 2**19937-1), and passes the Big Crush test suite, with the
43 --  exception of the two linear complexity tests. Therefore, it is suitable for
44 --  simulations, but should not be used as a cryptographic pseudo-random source
45 --  without additional processing.
46
47 --  The design of this package effects is simplified compared to the design
48 --  of standard Ada.Numerics packages. There is no separate State type; the
49 --  Generator type itself suffices for this purpose. The parameter modes on
50 --  Reset procedures better reflect the effect of these routines.
51
52 with System.Random_Numbers;
53 with Interfaces; use Interfaces;
54
55 package GNAT.Random_Numbers is
56
57    type Generator is limited private;
58    subtype Initialization_Vector is
59      System.Random_Numbers.Initialization_Vector;
60
61    function Random (Gen : Generator) return Float;
62    function Random (Gen : Generator) return Long_Float;
63    --  Return pseudo-random numbers uniformly distributed on [0 .. 1)
64
65    function Random (Gen : Generator) return Interfaces.Integer_32;
66    function Random (Gen : Generator) return Interfaces.Unsigned_32;
67    function Random (Gen : Generator) return Interfaces.Integer_64;
68    function Random (Gen : Generator) return Interfaces.Unsigned_64;
69    function Random (Gen : Generator) return Integer;
70    function Random (Gen : Generator) return Long_Integer;
71    --  Return pseudo-random numbers uniformly distributed on T'First .. T'Last
72    --  for various builtin integer types.
73
74    generic
75       type Result_Subtype is (<>);
76       Default_Min : Result_Subtype := Result_Subtype'Val (0);
77    function Random_Discrete
78      (Gen   : Generator;
79       Min   : Result_Subtype := Default_Min;
80       Max   : Result_Subtype := Result_Subtype'Last) return Result_Subtype;
81    --  Returns pseudo-random numbers uniformly distributed on Min .. Max
82
83    generic
84       type Result_Subtype is digits <>;
85    function Random_Float (Gen   : Generator) return Result_Subtype;
86    --  Returns pseudo-random numbers uniformly distributed on [0 .. 1)
87
88    function Random_Gaussian (Gen : Generator) return Long_Float;
89    function Random_Gaussian (Gen : Generator) return Float;
90    --  Returns pseudo-random numbers normally distributed value with mean 0
91    --  and standard deviation 1.0.
92
93    procedure Reset (Gen : out Generator);
94    --  Re-initialize the state of Gen from the time of day
95
96    procedure Reset
97      (Gen       : out Generator;
98       Initiator : Initialization_Vector);
99    procedure Reset
100      (Gen       : out Generator;
101       Initiator : Interfaces.Integer_32);
102    procedure Reset
103      (Gen       : out Generator;
104       Initiator : Interfaces.Unsigned_32);
105    procedure Reset
106      (Gen       : out Generator;
107       Initiator : Integer);
108    --  Re-initialize Gen based on the Initiator in various ways. Identical
109    --  values of Initiator cause identical sequences of values.
110
111    procedure Reset (Gen : out Generator; From_State : Generator);
112    --  Causes the state of Gen to be identical to that of From_State; Gen
113    --  and From_State will produce identical sequences of values subsequently.
114
115    procedure Reset (Gen : out Generator; From_Image : String);
116    function Image (Gen : Generator) return String;
117    --  The call
118    --     Reset (Gen2, Image (Gen1))
119    --  has the same effect as Reset (Gen2, Gen1);
120
121    Max_Image_Width : constant :=
122      System.Random_Numbers.Max_Image_Width + 2 + 20 + 5;
123    --  Maximum possible length of result of Image (...)
124
125 private
126
127    type Generator is limited record
128       Rep : System.Random_Numbers.Generator;
129
130       Have_Gaussian : Boolean;
131       --  The algorithm used for Random_Gaussian produces deviates in
132       --  pairs. Have_Gaussian is true iff Random_Gaussian has returned one
133       --  member of the pair and Next_Gaussian contains the other.
134
135       Next_Gaussian : Long_Float;
136       --  Next random deviate to be produced by Random_Gaussian, if
137       --  Have_Gaussian.
138    end record;
139
140 end GNAT.Random_Numbers;