Debian Codegraph @

gcc/ada/libgnat/s-rannum.ads

raw
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-2025, 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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- Extended pseudo-random number generation
33
34 -- This package provides a type representing pseudo-random number generators,
35 -- and subprograms to extract various uniform distributions of numbers
36 -- from them. It also provides types for representing initialization values
37 -- and snapshots of internal generator state, which permit reproducible
38 -- pseudo-random streams.
39
40 -- The generator currently provided by this package has an extremely long
41 -- period (at least 2**19937-1), and passes the Big Crush test suite, with the
42 -- exception of the two linear complexity tests. Therefore, it is suitable
43 -- for simulations, but should not be used as a cryptographic pseudo-random
44 -- source without additional processing.
45
46 -- Note: this package is in the System hierarchy so that it can be directly
47 -- used by other predefined packages. User access to this package is via
48 -- the package GNAT.Random_Numbers (file g-rannum.ads), which also extends
49 -- its capabilities. The interfaces are different so as to include in
50 -- System.Random_Numbers only the definitions necessary to implement the
51 -- standard random-number packages Ada.Numerics.Float_Random and
52 -- Ada.Numerics.Discrete_Random.
53
54 -- Note: this package is marked SPARK_Mode Off, because functions Random work
55 -- by side-effect to change the value of the generator, hence they should not
56 -- be called from SPARK code.
57
58 with Interfaces;
59
60 private with Ada.Strings.Text_Buffers;
61
62 package System.Random_Numbers with
63 SPARK_Mode => Off
64 is
65 type Generator is limited private;
66 -- Generator encodes the current state of a random number stream, it is
67 -- provided as input to produce the next random number, and updated so
68 -- that it is ready to produce the next one.
69
70 type State is private;
71 -- A non-limited version of a Generator's internal state
72
73 function Random (Gen : Generator) return Float;
74 function Random (Gen : Generator) return Long_Float;
75 -- Return pseudo-random numbers uniformly distributed on [0.0 .. 1.0)
76
77 function Random (Gen : Generator) return Interfaces.Unsigned_32;
78 function Random (Gen : Generator) return Interfaces.Unsigned_64;
79 function Random (Gen : Generator) return Interfaces.Unsigned_128;
80 -- Return pseudo-random numbers uniformly distributed on T'First .. T'Last
81 -- for builtin integer types.
82
83 generic
84 type Result_Subtype is (<>);
85 Default_Min : Result_Subtype := Result_Subtype'Val (0);
86 function Random_Discrete
87 (Gen : Generator;
88 Min : Result_Subtype := Default_Min;
89 Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype;
90 -- Returns pseudo-random numbers uniformly distributed on Min .. Max
91
92 generic
93 type Result_Subtype is digits <>;
94 function Random_Float (Gen : Generator) return Result_Subtype;
95 -- Returns pseudo-random numbers uniformly distributed on [0 .. 1)
96
97 type Initialization_Vector is
98 array (Integer range <>) of Interfaces.Unsigned_32;
99 -- Provides the most general initialization values for a generator (used
100 -- in Reset). In general, there is little point in providing more than
101 -- a certain number of values (currently 624).
102
103 procedure Reset (Gen : Generator);
104 -- Re-initialize the state of Gen from the time of day
105
106 procedure Reset (Gen : Generator; Initiator : Initialization_Vector);
107 procedure Reset (Gen : Generator; Initiator : Interfaces.Integer_32);
108 procedure Reset (Gen : Generator; Initiator : Interfaces.Unsigned_32);
109 procedure Reset (Gen : Generator; Initiator : Integer);
110 -- Re-initialize Gen based on the Initiator in various ways. Identical
111 -- values of Initiator cause identical sequences of values.
112
113 procedure Reset (Gen : Generator; From_State : Generator);
114 -- Causes the state of Gen to be identical to that of From_State; Gen
115 -- and From_State will produce identical sequences of values subsequently.
116
117 procedure Reset (Gen : Generator; From_State : State);
118 procedure Save (Gen : Generator; To_State : out State);
119 -- The sequence
120 -- Save (Gen2, S); Reset (Gen1, S)
121 -- has the same effect as Reset (Gen2, Gen1).
122
123 procedure Reset (Gen : Generator; From_Image : String);
124 function Image (Gen : Generator) return String;
125 -- The call
126 -- Reset (Gen2, Image (Gen1))
127 -- has the same effect as Reset (Gen2, Gen1);
128
129 Max_Image_Width : constant := 11 * 624;
130 -- Maximum possible length of result of Image (...)
131
132 function Image (Of_State : State) return String;
133 -- A String representation of Of_State. Identical to the result of
134 -- Image (Gen), if Of_State has been set with Save (Gen, Of_State).
135
136 function Value (Coded_State : String) return State;
137 -- Inverse of Image on States
138
139 private
140
141 N : constant := 624;
142 -- The number of 32-bit integers in the shift register
143
144 M : constant := 397;
145 -- Feedback distance from the current position
146
147 subtype State_Val is Interfaces.Unsigned_32;
148 type State is array (0 .. N - 1) of State_Val with Put_Image => Put_Image;
149
150 procedure Put_Image
151 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : State);
152
153 type Writable_Access (Self : access Generator) is limited null record;
154 -- Auxiliary type to make Generator a self-referential type
155
156 type Generator is limited record
157 Writable : Writable_Access (Generator'Access);
158 -- This self reference allows functions to modify Generator arguments
159
160 S : State := [others => 0];
161 -- The shift register, a circular buffer
162
163 I : Integer := N;
164 -- Current starting position in shift register S (N means uninitialized)
165 -- Naming exception: I is fine to use here as it is the name used in the
166 -- original paper describing the Mersenne Twister and in common
167 -- descriptions of the algorithm.
168 end record;
169
170 end System.Random_Numbers;