1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . N U M E R I C S . D I S C R E T E _ R A N D O M --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
34 with Interfaces
; use Interfaces
;
36 package body Ada
.Numerics
.Discrete_Random
is
38 -------------------------
39 -- Implementation Note --
40 -------------------------
42 -- The design of this spec is very awkward, as a result of Ada 95 not
43 -- permitting in-out parameters for function formals (most naturally
44 -- Generator values would be passed this way). In pure Ada 95, the only
45 -- solution is to use the heap and pointers, and, to avoid memory leaks,
48 -- This is awfully heavy, so what we do is to use Unrestricted_Access to
49 -- get a pointer to the state in the passed Generator. This works because
50 -- Generator is a limited type and will thus always be passed by reference.
52 type Pointer
is access all State
;
54 Fits_In_32_Bits
: constant Boolean :=
56 or else (Rst
'Size = 31
57 and then Rst
'Pos (Rst
'First) < 0);
58 -- This is set True if we do not need more than 32 bits in the result. If
59 -- we need 64-bits, we will only use the meaningful 48 bits of any 64-bit
60 -- number generated, since if more than 48 bits are required, we split the
61 -- computation into two separate parts, since the algorithm does not behave
64 -- The way this expression works is that obviously if the size is 31 bits,
65 -- it fits in 32 bits. In the 32-bit case, it fits in 32-bit signed if the
66 -- range has negative values. It is too conservative in the case that the
67 -- programmer has set a size greater than the default, e.g. a size of 33
68 -- for an integer type with a range of 1..10, but an over-conservative
69 -- result is OK. The important thing is that the value is only True if
70 -- we know the result will fit in 32-bits signed. If the value is False
71 -- when it could be True, the behavior will be correct, just a bit less
72 -- efficient than it could have been in some unusual cases.
74 -- One might assume that we could get a more accurate result by testing
75 -- the lower and upper bounds of the type Rst against the bounds of 32-bit
76 -- Integer. However, there is no easy way to do that. Why? Because in the
77 -- relatively rare case where this expresion has to be evaluated at run
78 -- time rather than compile time (when the bounds are dynamic), we need a
79 -- type to use for the computation. But the possible range of upper bound
80 -- values for Rst (remembering the possibility of 64-bit modular types) is
81 -- from -2**63 to 2**64-1, and no run-time type has a big enough range.
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 function Square_Mod_N
(X
, N
: Int
) return Int
;
88 pragma Inline
(Square_Mod_N
);
89 -- Computes X**2 mod N avoiding intermediate overflow
95 function Image
(Of_State
: State
) return String is
97 return Int
'Image (Of_State
.X1
) &
99 Int
'Image (Of_State
.X2
) &
101 Int
'Image (Of_State
.Q
);
108 function Random
(Gen
: Generator
) return Rst
is
109 Genp
: constant Pointer
:= Gen
.Gen_State
'Unrestricted_Access;
114 -- Check for flat range here, since we are typically run with checks
115 -- off, note that in practice, this condition will usually be static
116 -- so we will not actually generate any code for the normal case.
118 if Rst
'Last < Rst
'First then
119 raise Constraint_Error
;
122 -- Continue with computation if non-flat range
124 Genp
.X1
:= Square_Mod_N
(Genp
.X1
, Genp
.P
);
125 Genp
.X2
:= Square_Mod_N
(Genp
.X2
, Genp
.Q
);
126 Temp
:= Genp
.X2
- Genp
.X1
;
128 -- Following duplication is not an error, it is a loop unwinding!
131 Temp
:= Temp
+ Genp
.Q
;
135 Temp
:= Temp
+ Genp
.Q
;
138 TF
:= Offs
+ (Flt
(Temp
) * Flt
(Genp
.P
) + Flt
(Genp
.X1
)) * Genp
.Scl
;
140 -- Pathological, but there do exist cases where the rounding implicit
141 -- in calculating the scale factor will cause rounding to 'Last + 1.
142 -- In those cases, returning 'First results in the least bias.
144 if TF
>= Flt
(Rst
'Pos (Rst
'Last)) + 0.5 then
147 elsif not Fits_In_32_Bits
then
148 return Rst
'Val (Interfaces
.Integer_64
(TF
));
151 return Rst
'Val (Int
(TF
));
159 procedure Reset
(Gen
: Generator
; Initiator
: Integer) is
160 Genp
: constant Pointer
:= Gen
.Gen_State
'Unrestricted_Access;
164 X1
:= 2 + Int
(Initiator
) mod (K1
- 3);
165 X2
:= 2 + Int
(Initiator
) mod (K2
- 3);
168 X1
:= Square_Mod_N
(X1
, K1
);
169 X2
:= Square_Mod_N
(X2
, K2
);
172 -- Eliminate effects of small Initiators
187 procedure Reset
(Gen
: Generator
) is
188 Genp
: constant Pointer
:= Gen
.Gen_State
'Unrestricted_Access;
189 Now
: constant Calendar
.Time
:= Calendar
.Clock
;
194 X1
:= Int
(Calendar
.Year
(Now
)) * 12 * 31 +
195 Int
(Calendar
.Month
(Now
) * 31) +
196 Int
(Calendar
.Day
(Now
));
198 X2
:= Int
(Calendar
.Seconds
(Now
) * Duration (1000.0));
200 X1
:= 2 + X1
mod (K1
- 3);
201 X2
:= 2 + X2
mod (K2
- 3);
203 -- Eliminate visible effects of same day starts
206 X1
:= Square_Mod_N
(X1
, K1
);
207 X2
:= Square_Mod_N
(X2
, K2
);
224 procedure Reset
(Gen
: Generator
; From_State
: State
) is
225 Genp
: constant Pointer
:= Gen
.Gen_State
'Unrestricted_Access;
227 Genp
.all := From_State
;
234 procedure Save
(Gen
: Generator
; To_State
: out State
) is
236 To_State
:= Gen
.Gen_State
;
243 function Square_Mod_N
(X
, N
: Int
) return Int
is
245 return Int
((Integer_64
(X
) ** 2) mod (Integer_64
(N
)));
252 function Value
(Coded_State
: String) return State
is
253 Last
: constant Natural := Coded_State
'Last;
254 Start
: Positive := Coded_State
'First;
255 Stop
: Positive := Coded_State
'First;
259 while Stop
<= Last
and then Coded_State
(Stop
) /= ',' loop
264 raise Constraint_Error
;
267 Outs
.X1
:= Int
'Value (Coded_State
(Start
.. Stop
- 1));
272 exit when Stop
> Last
or else Coded_State
(Stop
) = ',';
276 raise Constraint_Error
;
279 Outs
.X2
:= Int
'Value (Coded_State
(Start
.. Stop
- 1));
280 Outs
.Q
:= Int
'Value (Coded_State
(Stop
+ 1 .. Last
));
281 Outs
.P
:= Outs
.Q
* 2 + 1;
282 Outs
.FP
:= Flt
(Outs
.P
);
283 Outs
.Scl
:= (RstL
- RstF
+ 1.0) / (Flt
(Outs
.P
) * Flt
(Outs
.Q
));
285 -- Now do *some* sanity checks
288 or else Outs
.X1
not in 2 .. Outs
.P
- 1
289 or else Outs
.X2
not in 2 .. Outs
.Q
- 1
291 raise Constraint_Error
;
297 end Ada
.Numerics
.Discrete_Random
;