From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,70c288b84e649469 X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: need help in random numbers. Date: 1997/06/13 Message-ID: #1/1 X-Deja-AN: 248857501 References: <339FFFCD.B71A2392@telem.openu.ac.il> Organization: The Mitre Corp., Bedford, MA. Newsgroups: comp.lang.ada Date: 1997-06-13T00:00:00+00:00 List-Id: In article <339FFFCD.B71A2392@telem.openu.ac.il> Amos Ortal 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...