-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomnumber.adb
73 lines (62 loc) · 2.31 KB
/
randomnumber.adb
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;
package body RandomNumber is
-- a random number generator
Seed : Generator;
-- Taken from: http://rosettacode.org/wiki/Random_numbers#Ada and
-- corrected to use ln instead of log base 10
function NormalFloat(Mu : Float;
Sigma : Float) return Float is
Result : Float;
begin
Reset(Seed);
Result := Mu + (Sigma * Sqrt (-2.0 * Log (Random (Seed))) *
Cos (2.0 * Pi * Random (Seed)));
return Result;
end NormalFloat;
function NormalInteger(Mu : Integer;
Sigma : Integer) return Integer is
begin
return Integer(NormalFloat(Float(Mu), Float(Sigma)));
end NormalInteger;
function UniformFloat(Lower, Upper : Float) return Float is
Base : Uniformly_Distributed;
Result : Float;
begin
Reset(Seed);
Base := Random(Seed);
Result := Lower + (Upper - Lower) * Base;
return Result;
end UniformFloat;
function UniformInteger(Lower, Upper : Integer) return Integer is
begin
return Integer(UniformFloat(Float(Lower), Float(Upper)));
end UniformInteger;
function UniformFloatWithError(Value : in Float; Error : in Float)
return Float is
Lower, Upper : Float; -- The lower and upper bounds given the error
RandomValue : Float;
begin
Reset(Seed);
Lower := Value * (1.0 - Error);
Upper := Value * (1.0 + Error);
RandomValue := UniformFloat(Lower, Upper);
return RandomValue;
end UniformFloatWithError;
function UniformIntegerWithError(Value : in Integer; Error : in Float)
return Integer is
begin
return Integer(UniformFloatWithError(Float(Value), Error));
end UniformIntegerWithError;
function RandomBooleanWithBias(Prob : in Uniformly_Distributed)
return Boolean is
begin
return Random(Seed) <= Prob;
end RandomBooleanWithBias;
function RandomBooleanUnbiased return Boolean is
begin
return RandomBooleanWithBias(0.5);
end RandomBooleanUnbiased;
end RandomNumber;