Merged with mainline at revision 128810.
[official-gcc.git] / gcc / ada / s-rannum.ads
blob28e2c9e8663eb2f7aedbf4c3a00f16385882c8e0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . 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 ------------------------------------------------------------------------------
34 -- Extended pseudo-random number generation
36 -- This package provides a type representing pseudo-random number generators,
37 -- and subprograms to extract various uniform distributions of numbers
38 -- from them. It also provides types for representing initialization values
39 -- and snapshots of internal generator state, which permit reproducible
40 -- pseudo-random streams.
42 -- The generator currently provided by this package has an extremely long
43 -- period (at least 2**19937-1), and passes the Big Crush test suite, with the
44 -- exception of the two linear complexity tests. Therefore, it is suitable
45 -- for simulations, but should not be used as a cryptographic pseudo-random
46 -- source without additional processing.
48 -- Note: this package is in the System hierarchy so that it can be directly
49 -- used by other predefined packages. User access to this package is via
50 -- the package GNAT.Random_Numbers (file g-rannum.ads), which also extends
51 -- its capabilities. The interfaces are different so as to include in
52 -- System.Random_Numbers only the definitions necessary to implement the
53 -- standard random-number packages Ada.Numerics.Float_Random and
54 -- Ada.Numerics.Discrete_Random.
56 with Interfaces;
58 package System.Random_Numbers is
60 type Generator is limited private;
61 type State is private;
62 -- A non-limited version of a Generator's internal state
64 function Random (Gen : Generator) return Float;
65 function Random (Gen : Generator) return Long_Float;
66 -- Return pseudo-random numbers uniformly distributed on [0 .. 1)
68 function Random (Gen : Generator) return Interfaces.Unsigned_32;
69 function Random (Gen : Generator) return Interfaces.Unsigned_64;
70 -- Return pseudo-random numbers uniformly distributed on T'First .. T'Last
71 -- for builtin integer types.
73 generic
74 type Result_Subtype is (<>);
75 Default_Min : Result_Subtype := Result_Subtype'Val (0);
76 function Random_Discrete
77 (Gen : Generator;
78 Min : Result_Subtype := Default_Min;
79 Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype;
80 -- Returns pseudo-random numbers uniformly distributed on Min .. Max
82 generic
83 type Result_Subtype is digits <>;
84 function Random_Float (Gen : Generator) return Result_Subtype;
85 -- Returns pseudo-random numbers uniformly distributed on [0 .. 1)
87 type Initialization_Vector is
88 array (Integer range <>) of Interfaces.Unsigned_32;
89 -- Provides the most general initialization values for a generator (used
90 -- in Reset). In general, there is little point in providing more than
91 -- a certain number of values (currently 624).
93 procedure Reset (Gen : out Generator);
94 -- Re-initialize the state of Gen from the time of day
96 procedure Reset (Gen : out Generator; Initiator : Initialization_Vector);
97 procedure Reset (Gen : out Generator; Initiator : Interfaces.Integer_32);
98 procedure Reset (Gen : out Generator; Initiator : Interfaces.Unsigned_32);
99 procedure Reset (Gen : out Generator; Initiator : Integer);
100 -- Re-initialize Gen based on the Initiator in various ways. Identical
101 -- values of Initiator cause identical sequences of values.
103 procedure Reset (Gen : out Generator; From_State : Generator);
104 -- Causes the state of Gen to be identical to that of From_State; Gen
105 -- and From_State will produce identical sequences of values subsequently.
107 procedure Reset (Gen : out Generator; From_State : State);
108 procedure Save (Gen : Generator; To_State : out State);
109 -- The sequence
110 -- Save (Gen2, S); Reset (Gen1, S)
111 -- has the same effect as Reset (Gen2, Gen1).
113 procedure Reset (Gen : out Generator; From_Image : String);
114 function Image (Gen : Generator) return String;
115 -- The call
116 -- Reset (Gen2, Image (Gen1))
117 -- has the same effect as Reset (Gen2, Gen1);
119 Max_Image_Width : constant := 11 * 624;
120 -- Maximum possible length of result of Image (...)
122 function Image (Of_State : State) return String;
123 -- A String representation of Of_State. Identical to the result of
124 -- Image (Gen), if Of_State has been set with Save (Gen, Of_State).
126 function Value (Coded_State : String) return State;
127 -- Inverse of Image on States
129 private
131 N : constant := 624;
132 -- The number of 32-bit integers in the shift register
134 M : constant := 397;
135 -- Feedback distance from the current position
137 subtype State_Val is Interfaces.Unsigned_32;
138 type State is array (0 .. N - 1) of State_Val;
140 type Generator is limited record
141 S : State := (others => 0);
142 -- The shift register, a circular buffer
144 I : Integer := N;
145 -- Current starting position in shift register S
146 end record;
148 end System.Random_Numbers;