comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Random numbers in Gnat
Date: 02 Mar 1995 15:50:15 GMT
Date: 1995-03-02T15:50:15+00:00	[thread overview]
Message-ID: <EACHUS.95Mar2105015@spectre.mitre.org> (raw)
In-Reply-To: ab@captblood.armstrong.edu's message of 1 Mar 1995 21:51:57 GMT

In article <3j2q9t$s3n@solar.Armstrong.EDU> ab@captblood.armstrong.edu () writes:

 > with text_io; use text_io;
 > with ada.numerics.random; use ada.numerics.random;
 >   procedure randtest is
 >   package int_io is new integer_io(integer); use int_io;

 >	     rand_state: state:= make_state;

--     This initialization is wrong! The GNAT people will have to figure
-- out why it got through.  It seems to be a call to the function
-- Make_State in the private package Ada.Numerics.Random.

 >     -- return random number in 0..up_bound
 >     function my_random(up_bound: integer) return integer is
 >	   r: Uniformly_Distributed;
 >     begin
 >	   random( rand_state, r);
 >	   return integer(r*float(up_bound));
 >     end my_random;          
 >   begin

-- At this point you can insert a call to Reset, in theory it is not
-- needed, but the current generator seems to have some problems with
-- degenerate seeds.  I used:

       Reset(rand_state, 1234);
 >     for i in 1..10 loop
 >	   put(my_random(1000)); new_line;
 >     end loop;
 >   end randtest;

-- Under separate cover I'll send you a better generator. ;-)

 > The program prints out only zeros rather than 10 random numbers in the
 > 0..1000 range. I dislike the use of the global rand_state variable. 
 > However using it as a parameter is no better either. Indeed, why
 > should one be bothered with this quantity anyway? (In C one
 > just seeds the generator, and then calls the random() as a 
 > parameterless function, why not here?)

--  Because there is a serious issue involving tasking.  There are two
-- solutions in the tasking case: a single state with atomic updates,
-- or a state per task.  The solution chosen was to allow the Ada
-- language to be used and not force the wrong choice on users.  In
-- the non-tasking case, a pair of declarations like:

   My_Gen: Ada.Numerics.Float_Random.Generator;
   function Rand(Gen: in Ada.Numerics.Float_Random.Generator := My_Gen)
      return Ada.Numerics.Float_Random.Uniformly_Distributed renames
      Ada.Numerics.Float_Random.Random;

--    And all the heavy breathing is in one place.  (This particular
-- example does make a powerful argument for use clauses though.)

> I'll appreciate your discussion as to how the random number
> generator should be used. Pointers to some documentation/discussion
> of the objects in a-numran.ads would also be helpful.

-- Also by e-mail I'll send some documentation of how the generator I
-- use works.


--

					Robert I. Eachus

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



  parent reply	other threads:[~1995-03-02 15:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-01 21:51 Random numbers in Gnat ab
1995-03-02  8:31 ` Dave Marshall
1995-03-03 19:11   ` Robert Dewar
1995-03-02 15:50 ` Robert I. Eachus [this message]
1995-03-03 15:08 ` Tucker Taft
replies disabled

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