comp.lang.ada
 help / color / mirror / Atom feed
* need help in random numbers.
@ 1997-06-12  0:00 Amos Ortal
  1997-06-13  0:00 ` Robert I. Eachus
  0 siblings, 1 reply; 2+ messages in thread
From: Amos Ortal @ 1997-06-12  0:00 UTC (permalink / raw)



Hello people.

How do I create random numbers, in ada.calendar.time type?
-- 
		_ _
Amos Ortal      O O
		 |
		 -




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: need help in random numbers.
  1997-06-12  0:00 need help in random numbers Amos Ortal
@ 1997-06-13  0:00 ` Robert I. Eachus
  0 siblings, 0 replies; 2+ messages in thread
From: Robert I. Eachus @ 1997-06-13  0:00 UTC (permalink / raw)



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...




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-06-13  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-06-12  0:00 need help in random numbers Amos Ortal
1997-06-13  0:00 ` Robert I. Eachus

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