1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . R A N D O M _ N U M B E R S --
9 -- Copyright (C) 2007, Free Software Foundation, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- Extended pseudo-random number generation
36 -- This package provides a type representing pseudo-random number
37 -- generators, and subprograms to extract various distributions of numbers
38 -- from them. It also provides types for representing initialization values
39 -- and snapshots of internal generator state, which permit reproducible
40 -- pseudo-random streams.
42 -- The generator currently provided by this package has an extremely long
43 -- period (at least 2**19937-1), and passes the Big Crush test suite, with
44 -- the exception of the two linear complexity tests. Therefore, it is
45 -- suitable for simulations, but should not be used as a cryptographic
46 -- pseudo-random source without additional processing.
48 -- The design of this package effects some simplification from that of
49 -- the standard Ada.Numerics packages. There is no separate State type;
50 -- the Generator type itself suffices for this purpose. The parameter
51 -- modes on Reset procedures better reflect the effect of these routines.
53 with System
.Random_Numbers
;
54 with Interfaces
; use Interfaces
;
56 package GNAT
.Random_Numbers
is
58 type Generator
is limited private;
59 subtype Initialization_Vector
is
60 System
.Random_Numbers
.Initialization_Vector
;
62 function Random
(Gen
: Generator
) return Float;
63 function Random
(Gen
: Generator
) return Long_Float;
64 -- Return pseudo-random numbers uniformly distributed on [0 .. 1)
66 function Random
(Gen
: Generator
) return Interfaces
.Integer_32
;
67 function Random
(Gen
: Generator
) return Interfaces
.Unsigned_32
;
68 function Random
(Gen
: Generator
) return Interfaces
.Integer_64
;
69 function Random
(Gen
: Generator
) return Interfaces
.Unsigned_64
;
70 function Random
(Gen
: Generator
) return Integer;
71 function Random
(Gen
: Generator
) return Long_Integer;
72 -- Return pseudo-random numbers uniformly distributed on T'First .. T'Last
73 -- for various builtin integer types.
76 type Result_Subtype
is (<>);
77 Default_Min
: Result_Subtype
:= Result_Subtype
'Val (0);
78 function Random_Discrete
80 Min
: Result_Subtype
:= Default_Min
;
81 Max
: Result_Subtype
:= Result_Subtype
'Last) return Result_Subtype
;
82 -- Returns pseudo-random numbers uniformly distributed on Min .. Max
85 type Result_Subtype
is digits <>;
86 function Random_Float
(Gen
: Generator
) return Result_Subtype
;
87 -- Returns pseudo-random numbers uniformly distributed on [0 .. 1)
89 function Random_Gaussian
(Gen
: Generator
) return Long_Float;
90 function Random_Gaussian
(Gen
: Generator
) return Float;
91 -- Returns pseudo-random numbers normally distributed value with mean 0
92 -- and standard deviation 1.0.
94 procedure Reset
(Gen
: out Generator
);
95 -- Re-initialize the state of Gen from the time of day
99 Initiator
: Initialization_Vector
);
101 (Gen
: out Generator
;
102 Initiator
: Interfaces
.Integer_32
);
104 (Gen
: out Generator
;
105 Initiator
: Interfaces
.Unsigned_32
);
107 (Gen
: out Generator
;
108 Initiator
: Integer);
109 -- Re-initialize Gen based on the Initiator in various ways. Identical
110 -- values of Initiator cause identical sequences of values.
112 procedure Reset
(Gen
: out Generator
; From_State
: Generator
);
113 -- Causes the state of Gen to be identical to that of From_State; Gen
114 -- and From_State will produce identical sequences of values subsequently.
116 procedure Reset
(Gen
: out Generator
; From_Image
: String);
117 function Image
(Gen
: Generator
) return String;
119 -- Reset (Gen2, Image (Gen1))
120 -- has the same effect as Reset (Gen2, Gen1);
122 Max_Image_Width
: constant :=
123 System
.Random_Numbers
.Max_Image_Width
+ 2 + 20 + 5;
124 -- Maximum possible length of result of Image (...)
128 type Generator
is limited record
129 Rep
: System
.Random_Numbers
.Generator
;
131 Have_Gaussian
: Boolean;
132 -- The algorithm used for Random_Gaussian produces deviates in
133 -- pairs. Have_Gaussian is true iff Random_Gaussian has returned one
134 -- member of the pair and Next_Gaussian contains the other.
136 Next_Gaussian
: Long_Float;
137 -- Next random deviate to be produced by Random_Gaussian, if
141 end GNAT
.Random_Numbers
;