1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT RUN-TIME COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- G N A T . R A N D O M _ N U M B E R S --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 2007-2009 Free Software Foundation, Inc. --
|
10 |
|
|
-- --
|
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. --
|
17 |
|
|
-- --
|
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. --
|
21 |
|
|
-- --
|
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/>. --
|
26 |
|
|
-- --
|
27 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
28 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
29 |
|
|
-- --
|
30 |
|
|
------------------------------------------------------------------------------
|
31 |
|
|
|
32 |
|
|
-- Extended pseudo-random number generation
|
33 |
|
|
|
34 |
|
|
-- This package provides a type representing pseudo-random number generators,
|
35 |
|
|
-- and subprograms to extract various distributions of numbers from them. It
|
36 |
|
|
-- also provides types for representing initialization values and snapshots of
|
37 |
|
|
-- internal generator state, which permit reproducible pseudo-random streams.
|
38 |
|
|
|
39 |
|
|
-- The generator currently provided by this package has an extremely long
|
40 |
|
|
-- period (at least 2**19937-1), and passes the Big Crush test suite, with the
|
41 |
|
|
-- exception of the two linear complexity tests. Therefore, it is suitable for
|
42 |
|
|
-- simulations, but should not be used as a cryptographic pseudo-random source
|
43 |
|
|
-- without additional processing.
|
44 |
|
|
|
45 |
|
|
-- The design of this package effects is simplified compared to the design
|
46 |
|
|
-- of standard Ada.Numerics packages. There is no separate State type; the
|
47 |
|
|
-- Generator type itself suffices for this purpose. The parameter modes on
|
48 |
|
|
-- Reset procedures better reflect the effect of these routines.
|
49 |
|
|
|
50 |
|
|
with System.Random_Numbers;
|
51 |
|
|
with Interfaces; use Interfaces;
|
52 |
|
|
|
53 |
|
|
package GNAT.Random_Numbers is
|
54 |
|
|
|
55 |
|
|
type Generator is limited private;
|
56 |
|
|
subtype Initialization_Vector is
|
57 |
|
|
System.Random_Numbers.Initialization_Vector;
|
58 |
|
|
|
59 |
|
|
function Random (Gen : Generator) return Float;
|
60 |
|
|
function Random (Gen : Generator) return Long_Float;
|
61 |
|
|
-- Return pseudo-random numbers uniformly distributed on [0 .. 1)
|
62 |
|
|
|
63 |
|
|
function Random (Gen : Generator) return Interfaces.Integer_32;
|
64 |
|
|
function Random (Gen : Generator) return Interfaces.Unsigned_32;
|
65 |
|
|
function Random (Gen : Generator) return Interfaces.Integer_64;
|
66 |
|
|
function Random (Gen : Generator) return Interfaces.Unsigned_64;
|
67 |
|
|
function Random (Gen : Generator) return Integer;
|
68 |
|
|
function Random (Gen : Generator) return Long_Integer;
|
69 |
|
|
-- Return pseudo-random numbers uniformly distributed on T'First .. T'Last
|
70 |
|
|
-- for various builtin integer types.
|
71 |
|
|
|
72 |
|
|
generic
|
73 |
|
|
type Result_Subtype is (<>);
|
74 |
|
|
Default_Min : Result_Subtype := Result_Subtype'Val (0);
|
75 |
|
|
function Random_Discrete
|
76 |
|
|
(Gen : Generator;
|
77 |
|
|
Min : Result_Subtype := Default_Min;
|
78 |
|
|
Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype;
|
79 |
|
|
-- Returns pseudo-random numbers uniformly distributed on Min .. Max
|
80 |
|
|
|
81 |
|
|
generic
|
82 |
|
|
type Result_Subtype is digits <>;
|
83 |
|
|
function Random_Float (Gen : Generator) return Result_Subtype;
|
84 |
|
|
-- Returns pseudo-random numbers uniformly distributed on [0 .. 1)
|
85 |
|
|
|
86 |
|
|
function Random_Gaussian (Gen : Generator) return Long_Float;
|
87 |
|
|
function Random_Gaussian (Gen : Generator) return Float;
|
88 |
|
|
-- Returns pseudo-random numbers normally distributed value with mean 0
|
89 |
|
|
-- and standard deviation 1.0.
|
90 |
|
|
|
91 |
|
|
procedure Reset (Gen : out Generator);
|
92 |
|
|
-- Re-initialize the state of Gen from the time of day
|
93 |
|
|
|
94 |
|
|
procedure Reset
|
95 |
|
|
(Gen : out Generator;
|
96 |
|
|
Initiator : Initialization_Vector);
|
97 |
|
|
procedure Reset
|
98 |
|
|
(Gen : out Generator;
|
99 |
|
|
Initiator : Interfaces.Integer_32);
|
100 |
|
|
procedure Reset
|
101 |
|
|
(Gen : out Generator;
|
102 |
|
|
Initiator : Interfaces.Unsigned_32);
|
103 |
|
|
procedure Reset
|
104 |
|
|
(Gen : out Generator;
|
105 |
|
|
Initiator : Integer);
|
106 |
|
|
-- Re-initialize Gen based on the Initiator in various ways. Identical
|
107 |
|
|
-- values of Initiator cause identical sequences of values.
|
108 |
|
|
|
109 |
|
|
procedure Reset (Gen : out Generator; From_State : Generator);
|
110 |
|
|
-- Causes the state of Gen to be identical to that of From_State; Gen
|
111 |
|
|
-- and From_State will produce identical sequences of values subsequently.
|
112 |
|
|
|
113 |
|
|
procedure Reset (Gen : out Generator; From_Image : String);
|
114 |
|
|
function Image (Gen : Generator) return String;
|
115 |
|
|
-- The call
|
116 |
|
|
-- Reset (Gen2, Image (Gen1))
|
117 |
|
|
-- has the same effect as Reset (Gen2, Gen1);
|
118 |
|
|
|
119 |
|
|
Max_Image_Width : constant :=
|
120 |
|
|
System.Random_Numbers.Max_Image_Width + 2 + 20 + 5;
|
121 |
|
|
-- Maximum possible length of result of Image (...)
|
122 |
|
|
|
123 |
|
|
private
|
124 |
|
|
|
125 |
|
|
type Generator is limited record
|
126 |
|
|
Rep : System.Random_Numbers.Generator;
|
127 |
|
|
|
128 |
|
|
Have_Gaussian : Boolean;
|
129 |
|
|
-- The algorithm used for Random_Gaussian produces deviates in
|
130 |
|
|
-- pairs. Have_Gaussian is true iff Random_Gaussian has returned one
|
131 |
|
|
-- member of the pair and Next_Gaussian contains the other.
|
132 |
|
|
|
133 |
|
|
Next_Gaussian : Long_Float;
|
134 |
|
|
-- Next random deviate to be produced by Random_Gaussian, if
|
135 |
|
|
-- Have_Gaussian.
|
136 |
|
|
end record;
|
137 |
|
|
|
138 |
|
|
end GNAT.Random_Numbers;
|