diff mbox

[Ada] Improve quality of PRNG seeding

Message ID 20120130114117.GA2657@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Jan. 30, 2012, 11:41 a.m. UTC
When seeding the System.Random_Numbers from the clock, the clock value used
to be truncated to whole seconds. This does not occur anymore, and the
full resolution of the clock value is now used.

Tested on x86_64-pc-linux-gnu, committed on trunk

2012-01-30  Thomas Quinot  <quinot@adacore.com>

	* s-rannum.adb, s-ransee.adb, s-ransee.ads (Get_Seed): Return
	Unsigned_64 rather than Duration.
	(System.Random_Numbers.Reset): Use full value of seed
	(do not truncate sub-second bits).
diff mbox

Patch

Index: s-rannum.adb
===================================================================
--- s-rannum.adb	(revision 183694)
+++ s-rannum.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 2007-2011, Free Software Foundation, Inc.         --
+--          Copyright (C) 2007-2012, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -480,12 +480,8 @@ 
    -----------
 
    procedure Reset (Gen : Generator) is
-      X : constant Unsigned_32 :=
-            Unsigned_32'Mod (Unsigned_64 (Random_Seed.Get_Seed) * 64);
-      --  Why * 64 ???
-
    begin
-      Init (Gen, X);
+      Init (Gen, Unsigned_32'Mod (Random_Seed.Get_Seed));
    end Reset;
 
    procedure Reset (Gen : Generator; Initiator : Integer_32) is
Index: s-ransee.adb
===================================================================
--- s-ransee.adb	(revision 183694)
+++ s-ransee.adb	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---               Copyright (C) 2011, Free Software Foundation, Inc.         --
+--          Copyright (C) 2011-2012, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -32,16 +32,24 @@ 
 --  Version used on all systems except Ravenscar where Calendar is unavailable
 
 with Ada.Calendar; use Ada.Calendar;
+with Ada.Unchecked_Conversion;
 
 package body System.Random_Seed is
 
    Y2K : constant Time :=
            Time_Of (Year => 2000, Month => 1, Day => 1, Seconds => 0.0);
-   --  First day of Year 2000, to get a duration.
+   --  First day of Year 2000, to get a duration
 
-   function Get_Seed return Duration is
+   function To_U64 is
+     new Ada.Unchecked_Conversion (Duration, Interfaces.Unsigned_64);
+
+   --------------
+   -- Get_Seed --
+   --------------
+
+   function Get_Seed return Interfaces.Unsigned_64 is
    begin
-      return Clock - Y2K;
+      return To_U64 (Clock - Y2K);
    end Get_Seed;
 
 end System.Random_Seed;
Index: s-ransee.ads
===================================================================
--- s-ransee.ads	(revision 183705)
+++ s-ransee.ads	(working copy)
@@ -6,7 +6,7 @@ 
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---            Copyright (C) 2011-2012, Free Software Foundation, Inc.       --
+--          Copyright (C) 2011-2012, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -39,9 +39,11 @@ 
 --  This is required because Ada.Calendar cannot be used on Ravenscar, but
 --  Ada.Real_Time drags in the whole tasking runtime on regular platforms.
 
+with Interfaces;
+
 package System.Random_Seed is
 
-   function Get_Seed return Duration;
+   function Get_Seed return Interfaces.Unsigned_64;
    --  Get a seed based on the clock
 
 end System.Random_Seed;