comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: Source for Random Number Generator
Date: 1997/02/07
Date: 1997-02-07T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.97Feb7182047@spectre.mitre.org> (raw)
In-Reply-To: EACHUS.97Feb6161130@spectre.mitre.org



   Following up my own posting, how gauche.  Oh, well I didn't want to
mislead anyone...

  >  with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
  >  function Biased_Random(Gen: in Generator) return Float is
  >  begin
  >    return Random(Gen) * 0.9 + 0.1;
  >  end Biased_Random;

  Ask for quick and dirty and get it.  I probably should have put a
smiley face on this.

  >    Now if you will settle for not so quick and dirty, I would
  > recommend:

  >  with Ada.Numerics.Float_Random;
  >  generic
  >    type Result_Type is digits <>;
  >    Gen: in out Ada.Numerics.Float_Random.Generator;
  >  function Generalized_Random return Result_Type;

  Do not instantiate this with an unconstrained floating-point type.
It will break, and I can see no reason slow things down to deal with
such a bogus choice.  But I probably should have put in a comment.

  >   function Generalized_Random return Result_Type is
  >	Temp: Result_Type;
  >   begin
  >     Temp := Result_Type(Float(Result_Type'First) +
  >	       Float(Result_Type'Last-Result_Type'First) * 
  >	       Ada.Numerics.Float_Random.Random(Gen));
  >	return Temp;
-- Temp is only used for expository purposes. On some hardware it may
-- be better to just say:
  >     return Result_Type(Float(Result_Type'First) +
  >	       Float(Result_Type'Last-Result_Type'First) * 
  >	       Ada.Numerics.Float_Random.Random(Gen));
-- But it depends on your compiler's inlining policy.
  >   exception when others => return Generalized_Random;
  >   -- rounding errors can result in a value of Temp outside the result
  >   -- subtype when the bounds are not model numbers.
  >   end Generalized_Random;

-- This comment is technically not true if the compiler doesn't
-- support the numerics annex or you are not running in strict mode.
-- But you still want the exception handler there.

      with Ada.Numerics.Float_Random; with Generalized_Random; with Text_IO;
      procedure Test_GR is
	subtype Result_Subtype is Float range 0.1..1.0;
	My_Gen: Ada.Numerics.Float_Random.Generator;
	function Rand is new Generalized_Random(Result_Subtype, My_Gen);
-- add pragma Inline for Rand here.
	package Floating_IO is new Text_IO.Float_IO(Result_Subtype);
      begin
	Text_IO.New_Line;
	for I in 1..10 loop
	  for J in 1..6 loop
	    Floating_IO.Put(Rand,3,9,0);
	  end loop;
	  Text_IO.New_Line;
	end loop;
      end Test_GR;

--

					Robert I. Eachus

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




  parent reply	other threads:[~1997-02-07  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <5d8b95$oec@zeus.orl.mmc.com>
1997-02-07  0:00 ` Source for Random Number Generator Nigel J. Tracey
1997-02-10  0:00   ` Jeff Carter
     [not found] ` <EACHUS.97Feb6161130@spectre.mitre.org>
1997-02-07  0:00   ` Robert I. Eachus [this message]
1997-02-10  0:00     ` VADSWorks X-Debugger Michael Levasseur
     [not found] ` <19970206153301.KAA04399@ladder01.news.aol.com>
1997-02-08  0:00   ` Source for Random Number Generator Robert I. Eachus
1997-02-11  0:00     ` johnherro
replies disabled

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