| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . R A N D O M _ N U M B E R S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2007-2025, 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 |
------------------------------------------------------------------------------ |
| 33 |
-- -- |
| 34 |
-- The implementation here is derived from a C-program for MT19937, with -- |
| 35 |
-- initialization improved 2002/1/26. As required, the following notice is -- |
| 36 |
-- copied from the original program. -- |
| 37 |
-- -- |
| 38 |
-- Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, -- |
| 39 |
-- All rights reserved. -- |
| 40 |
-- -- |
| 41 |
-- Redistribution and use in source and binary forms, with or without -- |
| 42 |
-- modification, are permitted provided that the following conditions -- |
| 43 |
-- are met: -- |
| 44 |
-- -- |
| 45 |
-- 1. Redistributions of source code must retain the above copyright -- |
| 46 |
-- notice, this list of conditions and the following disclaimer. -- |
| 47 |
-- -- |
| 48 |
-- 2. Redistributions in binary form must reproduce the above copyright -- |
| 49 |
-- notice, this list of conditions and the following disclaimer in the -- |
| 50 |
-- documentation and/or other materials provided with the distribution.-- |
| 51 |
-- -- |
| 52 |
-- 3. The names of its contributors may not be used to endorse or promote -- |
| 53 |
-- products derived from this software without specific prior written -- |
| 54 |
-- permission. -- |
| 55 |
-- -- |
| 56 |
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -- |
| 57 |
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -- |
| 58 |
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -- |
| 59 |
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -- |
| 60 |
-- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -- |
| 61 |
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED -- |
| 62 |
-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -- |
| 63 |
-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -- |
| 64 |
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -- |
| 65 |
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -- |
| 66 |
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- |
| 67 |
-- -- |
| 68 |
------------------------------------------------------------------------------ |
| 69 |
|
| 70 |
------------------------------------------------------------------------------ |
| 71 |
-- -- |
| 72 |
-- This is an implementation of the Mersenne Twister, twisted generalized -- |
| 73 |
-- feedback shift register of rational normal form, with state-bit -- |
| 74 |
-- reflection and tempering. This version generates 32-bit integers with a -- |
| 75 |
-- period of 2**19937 - 1 (a Mersenne prime, hence the name). For -- |
| 76 |
-- applications requiring more than 32 bits (up to 64), we concatenate two -- |
| 77 |
-- 32-bit numbers. -- |
| 78 |
-- -- |
| 79 |
-- See http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html for -- |
| 80 |
-- details. -- |
| 81 |
-- -- |
| 82 |
-- In contrast to the original code, we do not generate random numbers in -- |
| 83 |
-- batches of N. Measurement seems to show this has very little if any -- |
| 84 |
-- effect on performance, and it may be marginally better for real-time -- |
| 85 |
-- applications with hard deadlines. -- |
| 86 |
-- -- |
| 87 |
------------------------------------------------------------------------------ |
| 88 |
|
| 89 |
with Ada.Unchecked_Conversion; |
| 90 |
|
| 91 |
with System.Random_Seed; |
| 92 |
|
| 93 |
with Interfaces; use Interfaces; |
| 94 |
|
| 95 |
use Ada; |
| 96 |
|
| 97 |
package body System.Random_Numbers with |
| 98 |
SPARK_Mode => Off |
| 99 |
is |
| 100 |
Image_Numeral_Length : constant := Max_Image_Width / N; |
| 101 |
|
| 102 |
subtype Image_String is String (1 .. Max_Image_Width); |
| 103 |
|
| 104 |
---------------------------- |
| 105 |
-- Algorithmic Parameters -- |
| 106 |
---------------------------- |
| 107 |
|
| 108 |
Lower_Mask : constant := 2**31 - 1; |
| 109 |
Upper_Mask : constant := 2**31; |
| 110 |
|
| 111 |
Matrix_A : constant array (State_Val range 0 .. 1) of State_Val := |
| 112 |
[0, 16#9908b0df#]; |
| 113 |
-- The twist transformation is represented by a matrix of the form |
| 114 |
-- |
| 115 |
-- [ 0 I(31) ] |
| 116 |
-- [ _a ] |
| 117 |
-- |
| 118 |
-- where 0 is a 31x31 block of 0s, I(31) is the 31x31 identity matrix and |
| 119 |
-- _a is a particular bit row-vector, represented here by a 32-bit integer. |
| 120 |
-- If integer x represents a row vector of bits (with x(0), the units bit, |
| 121 |
-- last), then |
| 122 |
-- x * A = [0 x(31..1)] xor Matrix_A(x(0)). |
| 123 |
|
| 124 |
U : constant := 11; |
| 125 |
S : constant := 7; |
| 126 |
B_Mask : constant := 16#9d2c5680#; |
| 127 |
T : constant := 15; |
| 128 |
C_Mask : constant := 16#efc60000#; |
| 129 |
L : constant := 18; |
| 130 |
-- The tempering shifts and bit masks, in the order applied |
| 131 |
|
| 132 |
Seed0 : constant := 5489; |
| 133 |
-- Default seed, used to initialize the state vector when Reset not called |
| 134 |
|
| 135 |
Seed1 : constant := 19650218; |
| 136 |
-- Seed used to initialize the state vector when calling Reset with an |
| 137 |
-- initialization vector. |
| 138 |
|
| 139 |
Mult0 : constant := 1812433253; |
| 140 |
-- Multiplier for a modified linear congruential generator used to |
| 141 |
-- initialize the state vector when calling Reset with a single integer |
| 142 |
-- seed. |
| 143 |
|
| 144 |
Mult1 : constant := 1664525; |
| 145 |
Mult2 : constant := 1566083941; |
| 146 |
-- Multipliers for two modified linear congruential generators used to |
| 147 |
-- initialize the state vector when calling Reset with an initialization |
| 148 |
-- vector. |
| 149 |
|
| 150 |
----------------------- |
| 151 |
-- Local Subprograms -- |
| 152 |
----------------------- |
| 153 |
|
| 154 |
procedure Init (Gen : Generator; Initiator : Unsigned_32); |
| 155 |
-- Perform a default initialization of the state of Gen. The resulting |
| 156 |
-- state is identical for identical values of Initiator. |
| 157 |
|
| 158 |
procedure Insert_Image |
| 159 |
(S : in out Image_String; |
| 160 |
Index : Integer; |
| 161 |
V : State_Val); |
| 162 |
-- Insert image of V into S, in the Index'th 11-character substring |
| 163 |
|
| 164 |
function Extract_Value (S : String; Index : Integer) return State_Val; |
| 165 |
-- Treat S as a sequence of 11-character decimal numerals and return |
| 166 |
-- the result of converting numeral #Index (numbering from 0) |
| 167 |
|
| 168 |
function To_Unsigned is |
| 169 |
new Unchecked_Conversion (Integer_32, Unsigned_32); |
| 170 |
function To_Unsigned is |
| 171 |
new Unchecked_Conversion (Integer_64, Unsigned_64); |
| 172 |
|
| 173 |
------------ |
| 174 |
-- Random -- |
| 175 |
------------ |
| 176 |
|
| 177 |
function Random (Gen : Generator) return Unsigned_32 is |
| 178 |
G : Generator renames Gen.Writable.Self.all; |
| 179 |
Y : State_Val; |
| 180 |
I : Integer; |
| 181 |
-- Naming exception: I is fine to use here as it is the name used in |
| 182 |
-- the original paper describing the Mersenne Twister and in common |
| 183 |
-- descriptions of the algorithm. |
| 184 |
|
| 185 |
begin |
| 186 |
I := G.I; |
| 187 |
|
| 188 |
if I < N - M then |
| 189 |
Y := (G.S (I) and Upper_Mask) or (G.S (I + 1) and Lower_Mask); |
| 190 |
Y := G.S (I + M) xor Shift_Right (Y, 1) xor Matrix_A (Y and 1); |
| 191 |
I := I + 1; |
| 192 |
|
| 193 |
elsif I < N - 1 then |
| 194 |
Y := (G.S (I) and Upper_Mask) or (G.S (I + 1) and Lower_Mask); |
| 195 |
Y := G.S (I + (M - N)) |
| 196 |
xor Shift_Right (Y, 1) |
| 197 |
xor Matrix_A (Y and 1); |
| 198 |
I := I + 1; |
| 199 |
|
| 200 |
elsif I = N - 1 then |
| 201 |
Y := (G.S (I) and Upper_Mask) or (G.S (0) and Lower_Mask); |
| 202 |
Y := G.S (M - 1) xor Shift_Right (Y, 1) xor Matrix_A (Y and 1); |
| 203 |
I := 0; |
| 204 |
|
| 205 |
else |
| 206 |
Init (G, Seed0); |
| 207 |
return Random (Gen); |
| 208 |
end if; |
| 209 |
|
| 210 |
G.S (G.I) := Y; |
| 211 |
G.I := I; |
| 212 |
|
| 213 |
Y := Y xor Shift_Right (Y, U); |
| 214 |
Y := Y xor (Shift_Left (Y, S) and B_Mask); |
| 215 |
Y := Y xor (Shift_Left (Y, T) and C_Mask); |
| 216 |
Y := Y xor Shift_Right (Y, L); |
| 217 |
|
| 218 |
return Y; |
| 219 |
end Random; |
| 220 |
|
| 221 |
generic |
| 222 |
type Unsigned is mod <>; |
| 223 |
type Real is digits <>; |
| 224 |
with function Random (G : Generator) return Unsigned is <>; |
| 225 |
function Random_Float_Template (Gen : Generator) return Real; |
| 226 |
pragma Inline (Random_Float_Template); |
| 227 |
-- Template for a random-number generator implementation that delivers |
| 228 |
-- values of type Real in the range [0 .. 1], using values from Gen, |
| 229 |
-- assuming that Unsigned is large enough to hold the bits of a mantissa |
| 230 |
-- for type Real. |
| 231 |
|
| 232 |
--------------------------- |
| 233 |
-- Random_Float_Template -- |
| 234 |
--------------------------- |
| 235 |
|
| 236 |
function Random_Float_Template (Gen : Generator) return Real is |
| 237 |
|
| 238 |
pragma Compile_Time_Error |
| 239 |
(Unsigned'Last <= 2**(Real'Machine_Mantissa - 1), |
| 240 |
"insufficiently large modular type used to hold mantissa"); |
| 241 |
|
| 242 |
begin |
| 243 |
-- This code generates random floating-point numbers from unsigned |
| 244 |
-- integers. Assuming that Real'Machine_Radix = 2, it can deliver all |
| 245 |
-- machine values of type Real (as implied by Real'Machine_Mantissa and |
| 246 |
-- Real'Machine_Emin), which is not true of the standard method (to |
| 247 |
-- which we fall back for nonbinary radix): computing Real(<random |
| 248 |
-- integer>) / (<max random integer>+1). To do so, we first extract an |
| 249 |
-- (M-1)-bit significand (where M is Real'Machine_Mantissa), and then |
| 250 |
-- decide on a normalized exponent by repeated coin flips, decrementing |
| 251 |
-- from 0 as long as we flip heads (1 bits). This process yields the |
| 252 |
-- proper geometric distribution for the exponent: in a uniformly |
| 253 |
-- distributed set of floating-point numbers, 1/2 of them will be in |
| 254 |
-- (0.5, 1], 1/4 will be in (0.25, 0.5], and so forth. It makes a |
| 255 |
-- further adjustment at binade boundaries (see comments below) to give |
| 256 |
-- the effect of selecting a uniformly distributed real deviate in |
| 257 |
-- [0..1] and then rounding to the nearest representable floating-point |
| 258 |
-- number. The algorithm attempts to be stingy with random integers. In |
| 259 |
-- the worst case, it can consume roughly -Real'Machine_Emin/32 32-bit |
| 260 |
-- integers, but this case occurs with probability around |
| 261 |
-- 2**Machine_Emin, and the expected number of calls to integer-valued |
| 262 |
-- Random is 1. For another discussion of the issues addressed by this |
| 263 |
-- process, see Allen Downey's unpublished paper at |
| 264 |
-- http://allendowney.com/research/rand/downey07randfloat.pdf. |
| 265 |
|
| 266 |
if Real'Machine_Radix /= 2 then |
| 267 |
return Real'Machine |
| 268 |
(Real (Unsigned'(Random (Gen))) * 2.0**(-Unsigned'Size)); |
| 269 |
|
| 270 |
else |
| 271 |
declare |
| 272 |
type Bit_Count is range 0 .. 4; |
| 273 |
|
| 274 |
subtype T is Real'Base; |
| 275 |
|
| 276 |
Trailing_Ones : constant array (Unsigned_32 range 0 .. 15) |
| 277 |
of Bit_Count := |
| 278 |
[2#00000# => 0, 2#00001# => 1, 2#00010# => 0, 2#00011# => 2, |
| 279 |
2#00100# => 0, 2#00101# => 1, 2#00110# => 0, 2#00111# => 3, |
| 280 |
2#01000# => 0, 2#01001# => 1, 2#01010# => 0, 2#01011# => 2, |
| 281 |
2#01100# => 0, 2#01101# => 1, 2#01110# => 0, 2#01111# => 4]; |
| 282 |
|
| 283 |
Pow_Tab : constant array (Bit_Count range 0 .. 3) of Real := |
| 284 |
[0 => 2.0**(0 - T'Machine_Mantissa), |
| 285 |
1 => 2.0**(-1 - T'Machine_Mantissa), |
| 286 |
2 => 2.0**(-2 - T'Machine_Mantissa), |
| 287 |
3 => 2.0**(-3 - T'Machine_Mantissa)]; |
| 288 |
|
| 289 |
Extra_Bits : constant Natural := |
| 290 |
(Unsigned'Size - T'Machine_Mantissa + 1); |
| 291 |
-- Random bits left over after selecting mantissa |
| 292 |
|
| 293 |
Mantissa : Unsigned; |
| 294 |
|
| 295 |
X : Real; -- Scaled mantissa |
| 296 |
R : Unsigned_32; -- Supply of random bits |
| 297 |
R_Bits : Natural; -- Number of bits left in R |
| 298 |
K : Bit_Count; -- Next decrement to exponent |
| 299 |
|
| 300 |
begin |
| 301 |
K := 0; |
| 302 |
Mantissa := Random (Gen) / 2**Extra_Bits; |
| 303 |
R := Unsigned_32 (Mantissa mod 2**Extra_Bits); |
| 304 |
R_Bits := Extra_Bits; |
| 305 |
X := Real (2**(T'Machine_Mantissa - 1) + Mantissa); -- Exact |
| 306 |
|
| 307 |
if Extra_Bits < 4 and then R < 2 ** Extra_Bits - 1 then |
| 308 |
|
| 309 |
-- We got lucky and got a zero in our few extra bits |
| 310 |
|
| 311 |
K := Trailing_Ones (R); |
| 312 |
|
| 313 |
else |
| 314 |
Find_Zero : loop |
| 315 |
|
| 316 |
-- R has R_Bits unprocessed random bits, a multiple of 4. |
| 317 |
-- X needs to be halved for each trailing one bit. The |
| 318 |
-- process stops as soon as a 0 bit is found. If R_Bits |
| 319 |
-- becomes zero, reload R. |
| 320 |
|
| 321 |
-- Process 4 bits at a time for speed: the two iterations |
| 322 |
-- on average with three tests each was still too slow, |
| 323 |
-- probably because the branches are not predictable. |
| 324 |
-- This loop now will only execute once 94% of the cases, |
| 325 |
-- doing more bits at a time will not help. |
| 326 |
|
| 327 |
while R_Bits >= 4 loop |
| 328 |
K := Trailing_Ones (R mod 16); |
| 329 |
|
| 330 |
exit Find_Zero when K < 4; -- Exits 94% of the time |
| 331 |
|
| 332 |
R_Bits := R_Bits - 4; |
| 333 |
X := X / 16.0; |
| 334 |
R := R / 16; |
| 335 |
end loop; |
| 336 |
|
| 337 |
-- Do not allow us to loop endlessly even in the (very |
| 338 |
-- unlikely) case that Random (Gen) keeps yielding all ones. |
| 339 |
|
| 340 |
exit Find_Zero when X = 0.0; |
| 341 |
R := Random (Gen); |
| 342 |
R_Bits := 32; |
| 343 |
end loop Find_Zero; |
| 344 |
end if; |
| 345 |
|
| 346 |
-- K has the count of trailing ones not reflected yet in X. The |
| 347 |
-- following multiplication takes care of that, as well as the |
| 348 |
-- correction to move the radix point to the left of the mantissa. |
| 349 |
-- Doing it at the end avoids repeated rounding errors in the |
| 350 |
-- exceedingly unlikely case of ever having a subnormal result. |
| 351 |
|
| 352 |
X := X * Pow_Tab (K); |
| 353 |
|
| 354 |
-- The smallest value in each binade is rounded to by 0.75 of |
| 355 |
-- the span of real numbers as its next larger neighbor, and |
| 356 |
-- 1.0 is rounded to by half of the span of real numbers as its |
| 357 |
-- next smaller neighbor. To account for this, when we encounter |
| 358 |
-- the smallest number in a binade, we substitute the smallest |
| 359 |
-- value in the next larger binade with probability 1/2. |
| 360 |
|
| 361 |
if Mantissa = 0 and then Unsigned_32'(Random (Gen)) mod 2 = 0 then |
| 362 |
X := 2.0 * X; |
| 363 |
end if; |
| 364 |
|
| 365 |
return X; |
| 366 |
end; |
| 367 |
end if; |
| 368 |
end Random_Float_Template; |
| 369 |
|
| 370 |
------------ |
| 371 |
-- Random -- |
| 372 |
------------ |
| 373 |
|
| 374 |
function Random (Gen : Generator) return Float is |
| 375 |
function F is new Random_Float_Template (Unsigned_32, Float); |
| 376 |
begin |
| 377 |
return F (Gen); |
| 378 |
end Random; |
| 379 |
|
| 380 |
function Random (Gen : Generator) return Long_Float is |
| 381 |
function F is new Random_Float_Template (Unsigned_64, Long_Float); |
| 382 |
begin |
| 383 |
return F (Gen); |
| 384 |
end Random; |
| 385 |
|
| 386 |
function Random (Gen : Generator) return Unsigned_64 is |
| 387 |
begin |
| 388 |
return Shift_Left (Unsigned_64 (Unsigned_32'(Random (Gen))), 32) |
| 389 |
or Unsigned_64 (Unsigned_32'(Random (Gen))); |
| 390 |
end Random; |
| 391 |
|
| 392 |
function Random (Gen : Generator) return Unsigned_128 is |
| 393 |
begin |
| 394 |
return Shift_Left (Unsigned_128 (Unsigned_64'(Random (Gen))), 64) |
| 395 |
or Unsigned_128 (Unsigned_64'(Random (Gen))); |
| 396 |
end Random; |
| 397 |
|
| 398 |
--------------------- |
| 399 |
-- Random_Discrete -- |
| 400 |
--------------------- |
| 401 |
|
| 402 |
function Random_Discrete |
| 403 |
(Gen : Generator; |
| 404 |
Min : Result_Subtype := Default_Min; |
| 405 |
Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype |
| 406 |
is |
| 407 |
begin |
| 408 |
if Max = Min then |
| 409 |
return Max; |
| 410 |
|
| 411 |
elsif Max < Min then |
| 412 |
raise Constraint_Error; |
| 413 |
|
| 414 |
-- In the 128-bit case, we have to be careful since not all 128-bit |
| 415 |
-- unsigned values are representable in GNAT's universal integer. |
| 416 |
|
| 417 |
elsif Result_Subtype'Base'Size > 64 then |
| 418 |
declare |
| 419 |
-- Ignore unequal-size warnings since GNAT's handling is correct. |
| 420 |
|
| 421 |
pragma Warnings ("Z"); |
| 422 |
function Conv_To_Unsigned is |
| 423 |
new Unchecked_Conversion (Result_Subtype'Base, Unsigned_128); |
| 424 |
function Conv_To_Result is |
| 425 |
new Unchecked_Conversion (Unsigned_128, Result_Subtype'Base); |
| 426 |
pragma Warnings ("z"); |
| 427 |
|
| 428 |
N : constant Unsigned_128 := |
| 429 |
Conv_To_Unsigned (Max) - Conv_To_Unsigned (Min) + 1; |
| 430 |
|
| 431 |
X, Slop : Unsigned_128; |
| 432 |
|
| 433 |
begin |
| 434 |
if N = 0 then |
| 435 |
return Conv_To_Result (Conv_To_Unsigned (Min) + Random (Gen)); |
| 436 |
|
| 437 |
else |
| 438 |
Slop := Unsigned_128'Last rem N + 1; |
| 439 |
|
| 440 |
loop |
| 441 |
X := Random (Gen); |
| 442 |
exit when Slop = N or else X <= Unsigned_128'Last - Slop; |
| 443 |
end loop; |
| 444 |
|
| 445 |
return Conv_To_Result (Conv_To_Unsigned (Min) + X rem N); |
| 446 |
end if; |
| 447 |
end; |
| 448 |
|
| 449 |
-- In the 64-bit case, we have to be careful since not all 64-bit |
| 450 |
-- unsigned values are representable in GNAT's universal integer. |
| 451 |
|
| 452 |
elsif Result_Subtype'Base'Size > 32 then |
| 453 |
declare |
| 454 |
-- Ignore unequal-size warnings since GNAT's handling is correct. |
| 455 |
|
| 456 |
pragma Warnings ("Z"); |
| 457 |
function Conv_To_Unsigned is |
| 458 |
new Unchecked_Conversion (Result_Subtype'Base, Unsigned_64); |
| 459 |
function Conv_To_Result is |
| 460 |
new Unchecked_Conversion (Unsigned_64, Result_Subtype'Base); |
| 461 |
pragma Warnings ("z"); |
| 462 |
|
| 463 |
N : constant Unsigned_64 := |
| 464 |
Conv_To_Unsigned (Max) - Conv_To_Unsigned (Min) + 1; |
| 465 |
|
| 466 |
X, Slop : Unsigned_64; |
| 467 |
|
| 468 |
begin |
| 469 |
if N = 0 then |
| 470 |
return Conv_To_Result (Conv_To_Unsigned (Min) + Random (Gen)); |
| 471 |
|
| 472 |
else |
| 473 |
Slop := Unsigned_64'Last rem N + 1; |
| 474 |
|
| 475 |
loop |
| 476 |
X := Random (Gen); |
| 477 |
exit when Slop = N or else X <= Unsigned_64'Last - Slop; |
| 478 |
end loop; |
| 479 |
|
| 480 |
return Conv_To_Result (Conv_To_Unsigned (Min) + X rem N); |
| 481 |
end if; |
| 482 |
end; |
| 483 |
|
| 484 |
-- In the 32-bit case, we need to handle both integer and enumeration |
| 485 |
-- types and, therefore, rely on 'Pos and 'Val in the computation. |
| 486 |
|
| 487 |
elsif Result_Subtype'Pos (Max) - Result_Subtype'Pos (Min) = 2 ** 32 - 1 |
| 488 |
then |
| 489 |
return Result_Subtype'Val |
| 490 |
(Result_Subtype'Pos (Min) + Unsigned_32'Pos (Random (Gen))); |
| 491 |
|
| 492 |
else |
| 493 |
declare |
| 494 |
N : constant Unsigned_32 := |
| 495 |
Unsigned_32 (Result_Subtype'Pos (Max) - |
| 496 |
Result_Subtype'Pos (Min) + 1); |
| 497 |
Slop : constant Unsigned_32 := Unsigned_32'Last rem N + 1; |
| 498 |
X : Unsigned_32; |
| 499 |
|
| 500 |
begin |
| 501 |
loop |
| 502 |
X := Random (Gen); |
| 503 |
exit when Slop = N or else X <= Unsigned_32'Last - Slop; |
| 504 |
end loop; |
| 505 |
|
| 506 |
return |
| 507 |
Result_Subtype'Val |
| 508 |
(Result_Subtype'Pos (Min) + Unsigned_32'Pos (X rem N)); |
| 509 |
end; |
| 510 |
end if; |
| 511 |
end Random_Discrete; |
| 512 |
|
| 513 |
------------------ |
| 514 |
-- Random_Float -- |
| 515 |
------------------ |
| 516 |
|
| 517 |
function Random_Float (Gen : Generator) return Result_Subtype is |
| 518 |
begin |
| 519 |
if Result_Subtype'Base'Digits > Float'Digits then |
| 520 |
return Result_Subtype'Machine (Result_Subtype |
| 521 |
(Long_Float'(Random (Gen)))); |
| 522 |
else |
| 523 |
return Result_Subtype'Machine (Result_Subtype |
| 524 |
(Float'(Random (Gen)))); |
| 525 |
end if; |
| 526 |
end Random_Float; |
| 527 |
|
| 528 |
----------- |
| 529 |
-- Reset -- |
| 530 |
----------- |
| 531 |
|
| 532 |
procedure Reset (Gen : Generator) is |
| 533 |
begin |
| 534 |
Init (Gen, Unsigned_32'Mod (Random_Seed.Get_Seed)); |
| 535 |
end Reset; |
| 536 |
|
| 537 |
procedure Reset (Gen : Generator; Initiator : Integer_32) is |
| 538 |
begin |
| 539 |
Init (Gen, To_Unsigned (Initiator)); |
| 540 |
end Reset; |
| 541 |
|
| 542 |
procedure Reset (Gen : Generator; Initiator : Unsigned_32) is |
| 543 |
begin |
| 544 |
Init (Gen, Initiator); |
| 545 |
end Reset; |
| 546 |
|
| 547 |
procedure Reset (Gen : Generator; Initiator : Integer) is |
| 548 |
begin |
| 549 |
-- This is probably an unnecessary precaution against future change, but |
| 550 |
-- since the test is a static expression, no extra code is involved. |
| 551 |
|
| 552 |
if Integer'Size <= 32 then |
| 553 |
Init (Gen, To_Unsigned (Integer_32 (Initiator))); |
| 554 |
|
| 555 |
else |
| 556 |
declare |
| 557 |
Initiator1 : constant Unsigned_64 := |
| 558 |
To_Unsigned (Integer_64 (Initiator)); |
| 559 |
Init0 : constant Unsigned_32 := |
| 560 |
Unsigned_32 (Initiator1 mod 2 ** 32); |
| 561 |
Init1 : constant Unsigned_32 := |
| 562 |
Unsigned_32 (Shift_Right (Initiator1, 32)); |
| 563 |
begin |
| 564 |
Reset (Gen, Initialization_Vector'(Init0, Init1)); |
| 565 |
end; |
| 566 |
end if; |
| 567 |
end Reset; |
| 568 |
|
| 569 |
procedure Reset (Gen : Generator; Initiator : Initialization_Vector) is |
| 570 |
G : Generator renames Gen.Writable.Self.all; |
| 571 |
I, J : Integer; |
| 572 |
|
| 573 |
begin |
| 574 |
Init (G, Seed1); |
| 575 |
I := 1; |
| 576 |
J := 0; |
| 577 |
|
| 578 |
if Initiator'Length > 0 then |
| 579 |
for K in reverse 1 .. Integer'Max (N, Initiator'Length) loop |
| 580 |
G.S (I) := |
| 581 |
(G.S (I) xor ((G.S (I - 1) |
| 582 |
xor Shift_Right (G.S (I - 1), 30)) * Mult1)) |
| 583 |
+ Initiator (J + Initiator'First) + Unsigned_32 (J); |
| 584 |
|
| 585 |
I := I + 1; |
| 586 |
J := J + 1; |
| 587 |
|
| 588 |
if I >= N then |
| 589 |
G.S (0) := G.S (N - 1); |
| 590 |
I := 1; |
| 591 |
end if; |
| 592 |
|
| 593 |
if J >= Initiator'Length then |
| 594 |
J := 0; |
| 595 |
end if; |
| 596 |
end loop; |
| 597 |
end if; |
| 598 |
|
| 599 |
for K in reverse 1 .. N - 1 loop |
| 600 |
G.S (I) := |
| 601 |
(G.S (I) xor ((G.S (I - 1) |
| 602 |
xor Shift_Right (G.S (I - 1), 30)) * Mult2)) |
| 603 |
- Unsigned_32 (I); |
| 604 |
I := I + 1; |
| 605 |
|
| 606 |
if I >= N then |
| 607 |
G.S (0) := G.S (N - 1); |
| 608 |
I := 1; |
| 609 |
end if; |
| 610 |
end loop; |
| 611 |
|
| 612 |
G.S (0) := Upper_Mask; |
| 613 |
end Reset; |
| 614 |
|
| 615 |
procedure Reset (Gen : Generator; From_State : Generator) is |
| 616 |
G : Generator renames Gen.Writable.Self.all; |
| 617 |
begin |
| 618 |
G.S := From_State.S; |
| 619 |
G.I := From_State.I; |
| 620 |
end Reset; |
| 621 |
|
| 622 |
procedure Reset (Gen : Generator; From_State : State) is |
| 623 |
G : Generator renames Gen.Writable.Self.all; |
| 624 |
begin |
| 625 |
G.I := 0; |
| 626 |
G.S := From_State; |
| 627 |
end Reset; |
| 628 |
|
| 629 |
procedure Reset (Gen : Generator; From_Image : String) is |
| 630 |
G : Generator renames Gen.Writable.Self.all; |
| 631 |
begin |
| 632 |
G.I := 0; |
| 633 |
|
| 634 |
for J in 0 .. N - 1 loop |
| 635 |
G.S (J) := Extract_Value (From_Image, J); |
| 636 |
end loop; |
| 637 |
end Reset; |
| 638 |
|
| 639 |
---------- |
| 640 |
-- Save -- |
| 641 |
---------- |
| 642 |
|
| 643 |
procedure Save (Gen : Generator; To_State : out State) is |
| 644 |
Gen2 : Generator; |
| 645 |
|
| 646 |
begin |
| 647 |
if Gen.I = N then |
| 648 |
Init (Gen2, 5489); |
| 649 |
To_State := Gen2.S; |
| 650 |
|
| 651 |
else |
| 652 |
To_State (0 .. N - 1 - Gen.I) := Gen.S (Gen.I .. N - 1); |
| 653 |
To_State (N - Gen.I .. N - 1) := Gen.S (0 .. Gen.I - 1); |
| 654 |
end if; |
| 655 |
end Save; |
| 656 |
|
| 657 |
----------- |
| 658 |
-- Image -- |
| 659 |
----------- |
| 660 |
|
| 661 |
function Image (Of_State : State) return String is |
| 662 |
Result : Image_String; |
| 663 |
|
| 664 |
begin |
| 665 |
Result := [others => ' ']; |
| 666 |
|
| 667 |
for J in Of_State'Range loop |
| 668 |
Insert_Image (Result, J, Of_State (J)); |
| 669 |
end loop; |
| 670 |
|
| 671 |
return Result; |
| 672 |
end Image; |
| 673 |
|
| 674 |
function Image (Gen : Generator) return String is |
| 675 |
Result : Image_String; |
| 676 |
|
| 677 |
begin |
| 678 |
Result := [others => ' ']; |
| 679 |
for J in 0 .. N - 1 loop |
| 680 |
Insert_Image (Result, J, Gen.S ((J + Gen.I) mod N)); |
| 681 |
end loop; |
| 682 |
|
| 683 |
return Result; |
| 684 |
end Image; |
| 685 |
|
| 686 |
--------------- |
| 687 |
-- Put_Image -- |
| 688 |
--------------- |
| 689 |
|
| 690 |
procedure Put_Image |
| 691 |
(S : in out Strings.Text_Buffers.Root_Buffer_Type'Class; V : State) is |
| 692 |
begin |
| 693 |
Strings.Text_Buffers.Put (S, Image (V)); |
| 694 |
end Put_Image; |
| 695 |
|
| 696 |
----------- |
| 697 |
-- Value -- |
| 698 |
----------- |
| 699 |
|
| 700 |
function Value (Coded_State : String) return State is |
| 701 |
Gen : Generator; |
| 702 |
S : State; |
| 703 |
begin |
| 704 |
Reset (Gen, Coded_State); |
| 705 |
Save (Gen, S); |
| 706 |
return S; |
| 707 |
end Value; |
| 708 |
|
| 709 |
---------- |
| 710 |
-- Init -- |
| 711 |
---------- |
| 712 |
|
| 713 |
procedure Init (Gen : Generator; Initiator : Unsigned_32) is |
| 714 |
G : Generator renames Gen.Writable.Self.all; |
| 715 |
begin |
| 716 |
G.S (0) := Initiator; |
| 717 |
|
| 718 |
for I in 1 .. N - 1 loop |
| 719 |
G.S (I) := |
| 720 |
(G.S (I - 1) xor Shift_Right (G.S (I - 1), 30)) * Mult0 |
| 721 |
+ Unsigned_32 (I); |
| 722 |
end loop; |
| 723 |
|
| 724 |
G.I := 0; |
| 725 |
end Init; |
| 726 |
|
| 727 |
------------------ |
| 728 |
-- Insert_Image -- |
| 729 |
------------------ |
| 730 |
|
| 731 |
procedure Insert_Image |
| 732 |
(S : in out Image_String; |
| 733 |
Index : Integer; |
| 734 |
V : State_Val) |
| 735 |
is |
| 736 |
Value : constant String := State_Val'Image (V); |
| 737 |
begin |
| 738 |
S (Index * 11 + 1 .. Index * 11 + Value'Length) := Value; |
| 739 |
end Insert_Image; |
| 740 |
|
| 741 |
------------------- |
| 742 |
-- Extract_Value -- |
| 743 |
------------------- |
| 744 |
|
| 745 |
function Extract_Value (S : String; Index : Integer) return State_Val is |
| 746 |
Start : constant Integer := S'First + Index * Image_Numeral_Length; |
| 747 |
begin |
| 748 |
return State_Val'Value (S (Start .. Start + Image_Numeral_Length - 1)); |
| 749 |
end Extract_Value; |
| 750 |
|
| 751 |
end System.Random_Numbers; |