1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| -- boxmuller.ada V1.0 Implements the polar form of the Box-Muller
-- transformation.
-- presumes that RANDOM.UNIFORM(a,b) returns a uniformly distributed random
-- number in the range a..b
-- (c) Copyright 1994, Everett F. Carter Jr.
-- permission is granted by the author to use this software
-- for any application provided that this copyright notice
-- is preserved.
with RANDOM, Generic_Elementary_Functions;
function Box_Muller(m : in float; s : in float) return float is
x1, x2, w, y1 : float;
package float_funcs is new Generic_Elementary_Functions( float );
use float_funcs;
begin
loop
x1 := Random.Uniform(-1.0, 1.0);
x2 := Random.Uniform(-1.0, 1.0);
w := x1 * x1 + x2 * x2;
exit when w < 1.0;
end loop;
w := sqrt( ( -2.0 * log( w ) ) / w );
y1 := x1 * w;
return m + y1 * s;
end Box_Muller; |