1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- A D A . N U M E R I C S . F L O A T _ R A N D O M --
9 -- $Revision: 1.17 $ --
11 -- Copyright (C) 1992-1998, 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 ------------------------------------------------------------------------------
38 package body Ada.Numerics.Float_Random is
40 -------------------------
41 -- Implementation Note --
42 -------------------------
44 -- The design of this spec is very awkward, as a result of Ada 95 not
45 -- permitting in-out parameters for function formals (most naturally
46 -- Generator values would be passed this way). In pure Ada 95, the only
47 -- solution is to use the heap and pointers, and, to avoid memory leaks,
50 -- This is awfully heavy, so what we do is to use Unrestricted_Access to
51 -- get a pointer to the state in the passed Generator. This works because
52 -- Generator is a limited type and will thus always be passed by reference.
54 type Pointer is access all State;
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 procedure Euclid (P, Q : in Int; X, Y : out Int; GCD : out Int);
62 function Euclid (P, Q : Int) return Int;
64 function Square_Mod_N (X, N : Int) return Int;
70 procedure Euclid (P, Q : in Int; X, Y : out Int; GCD : out Int) is
76 (P, Q : in Int; -- a (i-1), a (i)
77 X, Y : in Int; -- x (i), y (i)
78 XP, YP : in out Int; -- x (i-1), y (i-1)
87 Quo : Int := P / Q; -- q <-- |_ a (i-1) / a (i) _|
88 XT : Int := X; -- x (i)
89 YT : Int := Y; -- y (i)
92 if P rem Q = 0 then -- while does not divide
97 Recur (Q, P - Q * Quo, XP - Quo * X, YP - Quo * Y, XT, YT, Quo);
100 -- a (i+1) <-- a (i-1) - q*a (i)
101 -- x (i+1) <-- x (i-1) - q*x (i)
102 -- y (i+1) <-- y (i-1) - q*y (i)
112 -- Start of processing for Euclid
115 Recur (P, Q, 0, 1, XT, YT, GCD);
120 function Euclid (P, Q : Int) return Int is
124 Euclid (P, Q, X, Y, GCD);
132 function Image (Of_State : State) return String is
134 return Int'Image (Of_State.X1) & ',' & Int'Image (Of_State.X2)
136 Int'Image (Of_State.P) & ',' & Int'Image (Of_State.Q);
143 function Random (Gen : Generator) return Uniformly_Distributed is
144 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
147 Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
148 Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
150 Float ((Flt (((Genp.X2 - Genp.X1) * Genp.X)
151 mod Genp.Q) * Flt (Genp.P)
152 + Flt (Genp.X1)) * Genp.Scl);
159 -- Version that works from given initiator value
161 procedure Reset (Gen : in Generator; Initiator : in Integer) is
162 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
166 X1 := 2 + Int (Initiator) mod (K1 - 3);
167 X2 := 2 + Int (Initiator) mod (K2 - 3);
169 -- Eliminate effects of small Initiators.
172 X1 := Square_Mod_N (X1, K1);
173 X2 := Square_Mod_N (X2, K2);
185 -- Version that works from specific saved state
187 procedure Reset (Gen : Generator; From_State : State) is
188 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
191 Genp.all := From_State;
194 -- Version that works from calendar
196 procedure Reset (Gen : Generator) is
197 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
198 Now : constant Calendar.Time := Calendar.Clock;
202 X1 := Int (Calendar.Year (Now)) * 12 * 31 +
203 Int (Calendar.Month (Now)) * 31 +
204 Int (Calendar.Day (Now));
206 X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
208 X1 := 2 + X1 mod (K1 - 3);
209 X2 := 2 + X2 mod (K2 - 3);
211 -- Eliminate visible effects of same day starts
214 X1 := Square_Mod_N (X1, K1);
215 X2 := Square_Mod_N (X2, K2);
233 procedure Save (Gen : in Generator; To_State : out State) is
235 To_State := Gen.Gen_State;
242 function Square_Mod_N (X, N : Int) return Int is
243 Temp : Flt := Flt (X) * Flt (X);
244 Div : Int := Int (Temp / Flt (N));
247 Div := Int (Temp - Flt (Div) * Flt (N));
260 function Value (Coded_State : String) return State is
261 Start : Positive := Coded_State'First;
262 Stop : Positive := Coded_State'First;
266 while Coded_State (Stop) /= ',' loop
270 Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
275 exit when Coded_State (Stop) = ',';
278 Outs.X2 := Int'Value (Coded_State (Start .. Stop - 1));
283 exit when Coded_State (Stop) = ',';
286 Outs.P := Int'Value (Coded_State (Start .. Stop - 1));
287 Outs.Q := Int'Value (Coded_State (Stop + 1 .. Coded_State'Last));
288 Outs.X := Euclid (Outs.P, Outs.Q);
289 Outs.Scl := 1.0 / (Flt (Outs.P) * Flt (Outs.Q));
291 -- Now do *some* sanity checks.
293 if Outs.Q < 31 or else Outs.P < 31
294 or else Outs.X1 not in 2 .. Outs.P - 1
295 or else Outs.X2 not in 2 .. Outs.Q - 1
297 raise Constraint_Error;
302 end Ada.Numerics.Float_Random;