annotate gcc/ada/libgnat/s-rannum.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT RUN-TIME COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- S Y S T E M . R A N D O M _ N U M B E R S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2007-2018, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- Extended pseudo-random number generation
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -- This package provides a type representing pseudo-random number generators,
kono
parents:
diff changeset
35 -- and subprograms to extract various uniform distributions of numbers
kono
parents:
diff changeset
36 -- from them. It also provides types for representing initialization values
kono
parents:
diff changeset
37 -- and snapshots of internal generator state, which permit reproducible
kono
parents:
diff changeset
38 -- pseudo-random streams.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 -- The generator currently provided by this package has an extremely long
kono
parents:
diff changeset
41 -- period (at least 2**19937-1), and passes the Big Crush test suite, with the
kono
parents:
diff changeset
42 -- exception of the two linear complexity tests. Therefore, it is suitable
kono
parents:
diff changeset
43 -- for simulations, but should not be used as a cryptographic pseudo-random
kono
parents:
diff changeset
44 -- source without additional processing.
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 -- Note: this package is in the System hierarchy so that it can be directly
kono
parents:
diff changeset
47 -- used by other predefined packages. User access to this package is via
kono
parents:
diff changeset
48 -- the package GNAT.Random_Numbers (file g-rannum.ads), which also extends
kono
parents:
diff changeset
49 -- its capabilities. The interfaces are different so as to include in
kono
parents:
diff changeset
50 -- System.Random_Numbers only the definitions necessary to implement the
kono
parents:
diff changeset
51 -- standard random-number packages Ada.Numerics.Float_Random and
kono
parents:
diff changeset
52 -- Ada.Numerics.Discrete_Random.
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 -- Note: this package is marked SPARK_Mode Off, because functions Random work
kono
parents:
diff changeset
55 -- by side-effect to change the value of the generator, hence they should not
kono
parents:
diff changeset
56 -- be called from SPARK code.
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 with Interfaces;
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 package System.Random_Numbers with
kono
parents:
diff changeset
61 SPARK_Mode => Off
kono
parents:
diff changeset
62 is
kono
parents:
diff changeset
63 type Generator is limited private;
kono
parents:
diff changeset
64 -- Generator encodes the current state of a random number stream, it is
kono
parents:
diff changeset
65 -- provided as input to produce the next random number, and updated so
kono
parents:
diff changeset
66 -- that it is ready to produce the next one.
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 type State is private;
kono
parents:
diff changeset
69 -- A non-limited version of a Generator's internal state
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 function Random (Gen : Generator) return Float;
kono
parents:
diff changeset
72 function Random (Gen : Generator) return Long_Float;
kono
parents:
diff changeset
73 -- Return pseudo-random numbers uniformly distributed on [0.0 .. 1.0)
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 function Random (Gen : Generator) return Interfaces.Unsigned_32;
kono
parents:
diff changeset
76 function Random (Gen : Generator) return Interfaces.Unsigned_64;
kono
parents:
diff changeset
77 -- Return pseudo-random numbers uniformly distributed on T'First .. T'Last
kono
parents:
diff changeset
78 -- for builtin integer types.
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 generic
kono
parents:
diff changeset
81 type Result_Subtype is (<>);
kono
parents:
diff changeset
82 Default_Min : Result_Subtype := Result_Subtype'Val (0);
kono
parents:
diff changeset
83 function Random_Discrete
kono
parents:
diff changeset
84 (Gen : Generator;
kono
parents:
diff changeset
85 Min : Result_Subtype := Default_Min;
kono
parents:
diff changeset
86 Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype;
kono
parents:
diff changeset
87 -- Returns pseudo-random numbers uniformly distributed on Min .. Max
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 generic
kono
parents:
diff changeset
90 type Result_Subtype is digits <>;
kono
parents:
diff changeset
91 function Random_Float (Gen : Generator) return Result_Subtype;
kono
parents:
diff changeset
92 -- Returns pseudo-random numbers uniformly distributed on [0 .. 1)
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 type Initialization_Vector is
kono
parents:
diff changeset
95 array (Integer range <>) of Interfaces.Unsigned_32;
kono
parents:
diff changeset
96 -- Provides the most general initialization values for a generator (used
kono
parents:
diff changeset
97 -- in Reset). In general, there is little point in providing more than
kono
parents:
diff changeset
98 -- a certain number of values (currently 624).
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 procedure Reset (Gen : Generator);
kono
parents:
diff changeset
101 -- Re-initialize the state of Gen from the time of day
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 procedure Reset (Gen : Generator; Initiator : Initialization_Vector);
kono
parents:
diff changeset
104 procedure Reset (Gen : Generator; Initiator : Interfaces.Integer_32);
kono
parents:
diff changeset
105 procedure Reset (Gen : Generator; Initiator : Interfaces.Unsigned_32);
kono
parents:
diff changeset
106 procedure Reset (Gen : Generator; Initiator : Integer);
kono
parents:
diff changeset
107 -- Re-initialize Gen based on the Initiator in various ways. Identical
kono
parents:
diff changeset
108 -- values of Initiator cause identical sequences of values.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 procedure Reset (Gen : Generator; From_State : Generator);
kono
parents:
diff changeset
111 -- Causes the state of Gen to be identical to that of From_State; Gen
kono
parents:
diff changeset
112 -- and From_State will produce identical sequences of values subsequently.
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 procedure Reset (Gen : Generator; From_State : State);
kono
parents:
diff changeset
115 procedure Save (Gen : Generator; To_State : out State);
kono
parents:
diff changeset
116 -- The sequence
kono
parents:
diff changeset
117 -- Save (Gen2, S); Reset (Gen1, S)
kono
parents:
diff changeset
118 -- has the same effect as Reset (Gen2, Gen1).
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 procedure Reset (Gen : Generator; From_Image : String);
kono
parents:
diff changeset
121 function Image (Gen : Generator) return String;
kono
parents:
diff changeset
122 -- The call
kono
parents:
diff changeset
123 -- Reset (Gen2, Image (Gen1))
kono
parents:
diff changeset
124 -- has the same effect as Reset (Gen2, Gen1);
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 Max_Image_Width : constant := 11 * 624;
kono
parents:
diff changeset
127 -- Maximum possible length of result of Image (...)
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 function Image (Of_State : State) return String;
kono
parents:
diff changeset
130 -- A String representation of Of_State. Identical to the result of
kono
parents:
diff changeset
131 -- Image (Gen), if Of_State has been set with Save (Gen, Of_State).
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 function Value (Coded_State : String) return State;
kono
parents:
diff changeset
134 -- Inverse of Image on States
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 private
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 N : constant := 624;
kono
parents:
diff changeset
139 -- The number of 32-bit integers in the shift register
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 M : constant := 397;
kono
parents:
diff changeset
142 -- Feedback distance from the current position
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 subtype State_Val is Interfaces.Unsigned_32;
kono
parents:
diff changeset
145 type State is array (0 .. N - 1) of State_Val;
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 type Writable_Access (Self : access Generator) is limited null record;
kono
parents:
diff changeset
148 -- Auxiliary type to make Generator a self-referential type
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 type Generator is limited record
kono
parents:
diff changeset
151 Writable : Writable_Access (Generator'Access);
kono
parents:
diff changeset
152 -- This self reference allows functions to modify Generator arguments
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 S : State := (others => 0);
kono
parents:
diff changeset
155 -- The shift register, a circular buffer
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 I : Integer := N;
kono
parents:
diff changeset
158 -- Current starting position in shift register S (N means uninitialized)
kono
parents:
diff changeset
159 -- We should avoid using the identifier I here ???
kono
parents:
diff changeset
160 end record;
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 end System.Random_Numbers;