comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: need help in random numbers.
Date: 1997/06/13
Date: 1997-06-13T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.97Jun13125939@spectre.mitre.org> (raw)
In-Reply-To: 339FFFCD.B71A2392@telem.openu.ac.il


In article <339FFFCD.B71A2392@telem.openu.ac.il> Amos Ortal <amosor@telem.openu.ac.il> writes:

  > How do I create random numbers, in ada.calendar.time type?

  Interesting problem, but let's first state it correctly:

  How do you create random values of type Ada.Calendar.Time?

  The immediate response is to ask:  With what distribution?  Over
what range?  And for what purpose?

  If the answers are uniform, Jan. 1, 1901..Dec. 31, 2099, and to test
year 2000 problems, I'd do the following:

  Look at the private part of Ada.Calendar, and discover the
representation of Time.  Write your own package, with an identical
declaration and use Unchecked_Coversion between the two.  In GNAT this
is pretty easy:

   type Time is new Duration;

   Determine the actual values for the range of interest (call them
FIRST_DATE and DOOM_DATE in this example), then write a package:

  with Ada.Calendar;
  package Random_Times is
    function Random_Time return Ada.Calendar.Time;
  end Random_Times;

  with Ada.Unchecked_Conversion;
  with Ada.Numerics.Float_Random;
  package body Random_Times is
  
    Gen: Ada.Numerics.Float_Random.Generator;

    function To_Duration is new
          Ada.Unchecked_Conversion(Ada.Calendar.Time,Duration);

    function To_Time is new
          Ada.Unchecked_Conversion(Duration,Ada.Calendar.Time);

    Doom_Date: constant Duration := 
           To_Duration(Ada.Calendar.Time_Of(2099,12,31,86399.99));
    First_Date: constant Duration := 
           To_Duration(Ada.Calendar.Time_Of(1901,1,1,0.0));
    Scale: constant Duration := Doom_Date-First_Date;
     -- Yes, I know that this could overflow, but in this example it
     -- won't and the work-around, if necessary, should be obvious.

    function Random_Time return Ada.Calendar.Time is
    begin
     return To_Time(First_Date + 
              Duration(Float(Scale) * Ada.Numerics.Float_Random.Random(Gen)));
    end Random_Time;

  begin
    Ada.Numerics.Float_Random.Reset(Gen);
    -- Don't forget this initialization call, or to a different
    -- version of Reset.
  end Random_Times;

  (Compiled, but not throughly tested.)






--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




      reply	other threads:[~1997-06-13  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-12  0:00 need help in random numbers Amos Ortal
1997-06-13  0:00 ` Robert I. Eachus [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox