1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME 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 --
11 -- Copyright (C) 1992-1999 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
37 with Interfaces; use Interfaces;
39 package body Ada.Numerics.Discrete_Random is
41 -------------------------
42 -- Implementation Note --
43 -------------------------
45 -- The design of this spec is very awkward, as a result of Ada 95 not
46 -- permitting in-out parameters for function formals (most naturally
47 -- Generator values would be passed this way). In pure Ada 95, the only
48 -- solution is to use the heap and pointers, and, to avoid memory leaks,
51 -- This is awfully heavy, so what we do is to use Unrestricted_Access to
52 -- get a pointer to the state in the passed Generator. This works because
53 -- Generator is a limited type and will thus always be passed by reference.
55 type Pointer is access all State;
57 Need_64 : constant Boolean := Rst'Pos (Rst'Last) > Int'Last;
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 function Square_Mod_N (X, N : Int) return Int;
64 pragma Inline (Square_Mod_N);
65 -- Computes X**2 mod N avoiding intermediate overflow
71 function Image (Of_State : State) return String is
73 return Int'Image (Of_State.X1) &
75 Int'Image (Of_State.X2) &
77 Int'Image (Of_State.Q);
84 function Random (Gen : Generator) return Rst is
85 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
90 -- Check for flat range here, since we are typically run with checks
91 -- off, note that in practice, this condition will usually be static
92 -- so we will not actually generate any code for the normal case.
94 if Rst'Last < Rst'First then
95 raise Constraint_Error;
98 -- Continue with computation if non-flat range
100 Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
101 Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
102 Temp := Genp.X2 - Genp.X1;
104 -- Following duplication is not an error, it is a loop unwinding!
107 Temp := Temp + Genp.Q;
111 Temp := Temp + Genp.Q;
114 TF := Offs + (Flt (Temp) * Flt (Genp.P) + Flt (Genp.X1)) * Genp.Scl;
116 -- Pathological, but there do exist cases where the rounding implicit
117 -- in calculating the scale factor will cause rounding to 'Last + 1.
118 -- In those cases, returning 'First results in the least bias.
120 if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
124 return Rst'Val (Interfaces.Integer_64 (TF));
127 return Rst'Val (Int (TF));
136 procedure Reset (Gen : Generator; Initiator : Integer) is
137 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
141 X1 := 2 + Int (Initiator) mod (K1 - 3);
142 X2 := 2 + Int (Initiator) mod (K2 - 3);
145 X1 := Square_Mod_N (X1, K1);
146 X2 := Square_Mod_N (X2, K2);
149 -- eliminate effects of small Initiators.
164 procedure Reset (Gen : Generator) is
165 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
166 Now : constant Calendar.Time := Calendar.Clock;
171 X1 := Int (Calendar.Year (Now)) * 12 * 31 +
172 Int (Calendar.Month (Now) * 31) +
173 Int (Calendar.Day (Now));
175 X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
177 X1 := 2 + X1 mod (K1 - 3);
178 X2 := 2 + X2 mod (K2 - 3);
180 -- Eliminate visible effects of same day starts
183 X1 := Square_Mod_N (X1, K1);
184 X2 := Square_Mod_N (X2, K2);
201 procedure Reset (Gen : Generator; From_State : State) is
202 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
205 Genp.all := From_State;
212 procedure Save (Gen : Generator; To_State : out State) is
214 To_State := Gen.Gen_State;
221 function Square_Mod_N (X, N : Int) return Int is
223 return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
230 function Value (Coded_State : String) return State is
231 Start : Positive := Coded_State'First;
232 Stop : Positive := Coded_State'First;
236 while Coded_State (Stop) /= ',' loop
240 Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
245 exit when Coded_State (Stop) = ',';
248 Outs.X2 := Int'Value (Coded_State (Start .. Stop - 1));
249 Outs.Q := Int'Value (Coded_State (Stop + 1 .. Coded_State'Last));
250 Outs.P := Outs.Q * 2 + 1;
251 Outs.FP := Flt (Outs.P);
252 Outs.Scl := (RstL - RstF + 1.0) / (Flt (Outs.P) * Flt (Outs.Q));
254 -- Now do *some* sanity checks.
257 or else Outs.X1 not in 2 .. Outs.P - 1
258 or else Outs.X2 not in 2 .. Outs.Q - 1
260 raise Constraint_Error;
266 end Ada.Numerics.Discrete_Random;