comp.lang.ada
 help / color / mirror / Atom feed
From: njt@minster.york.ac.uk (Nigel J. Tracey)
Subject: Re: Random Number Generation
Date: 1996/10/03
Date: 1996-10-03T00:00:00+00:00	[thread overview]
Message-ID: <530ieo$din@netty.york.ac.uk> (raw)
In-Reply-To: Dy7CMw.A7v.0.-s@inmet.camb.inmet.com


Robert I. Eachus (eachus@spectre.mitre.org) wrote:
: In article <52ten0$ish@netty.york.ac.uk> njt@minster.york.ac.uk (Nigel J. Tracey) writes:
: 
:   > I really need
:   > to be able to generate random values in the range of
:   > Float'Safe_First..Float'Safe_Last. Or perhaps a variable
:   > sub-range of these. Any idea...
: 
:   With what distribution?  If you are using this for testing
: arithmetic units, I suggest unchecked converting a string of random
: bits of the right length.  This will be a distribution which covers
: all representable numbers equally. (To save Robert Dewar the
: trouble...Certain bit patterns do not correspond to legal floating
: point values.  This a legitimate use for 'VALID).  However if you want
: a numerically uniform distribution:
: 
:    2.0 * Float'Safe_Last * (Ada.Numerics.Float_Random.Random(Gen) - 0.5);
: 
:    Actually you may want to add more bits to the typical floating
: point generator value.
: 
: 
: --
: 
: 					Robert I. Eachus

I have managed to write something which works with some limitations.
The problem this has is that if the range includes 0.0 and is not
sufficiently large then there is a stong bias towards 0.0. e.g
numbers in the range -1.0..1.0 are most often 0.0. However for
larger ranges or ranges not including 0.0 it seem okay. An ideas
on an improvement to the 0.0 bias...

The code is as follows (Random_Seed is declared in the package
body, it is initialised to a random integer on ellaboration
and incremented after each reset so that repeated calls to
the routine do no give non random results, due to granularity
of the reset(Gen) function):

with  Ada.Numerics.Discrete_Random,
      Ada.Numerics.Float_Random;

   function Random(Min, Max : Float) return Float is

      subtype Exponent_Type is Integer range
         Float'Machine_EMin..Float'Machine_EMax;

      package Exp_Random is new
         Ada.Numerics.Discrete_Random(Exponent_Type);

      Gen1 		: Ada.Numerics.Float_Random.Generator;
      Gen2 		: Exp_Random.Generator;
      Mantissa : Float range -1.0..1.0;
      Result   : Float;

   begin
      Ada.Numerics.Float_Random.Reset(Gen1, Random_Seed);
      Random_Seed := Random_Seed + 1;
      Exp_Random.Reset(Gen2, Random_Seed);
      Random_Seed := Random_Seed + 1;

      loop
         Mantissa := -1.0;
         Mantissa := Mantissa + Ada.Numerics.Float_Random.Random(Gen1);
         Mantissa := Mantissa + Ada.Numerics.Float_Random.Random(Gen1);

         Result := Float'Compose(Mantissa, Exp_Random.Random(Gen2));
      exit when Result > Min and Result < Max;
      end loop;

      return Result;
   end Random;





  parent reply	other threads:[~1996-10-03  0:00 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-23  0:00 Random Number Generation Nigel J. Tracey
1996-09-23  0:00 ` Tucker Taft
1996-10-02  0:00   ` Robert I. Eachus
1996-10-02  0:00   ` Nigel J. Tracey
1996-10-03  0:00   ` Nigel J. Tracey [this message]
1996-09-25  0:00 ` James_Rogers
1996-09-26  0:00   ` Dale Stanbrough
1996-10-01  0:00   ` Robert I. Eachus
1996-09-30  0:00 `  Dr J Parker
1996-10-01  0:00   ` Tucker Taft
1996-10-01  0:00     ` Keith Thompson
  -- strict thread matches above, loose matches on Subject: below --
1996-10-02  0:00  Dr J Parker
1996-10-03  0:00 ` Mats Weber
1996-10-07  0:00 ` Geert Bosch
1996-10-10  0:00  Dr J Parker
1996-10-10  0:00  Dr J Parker
1996-10-12  0:00 ` Geert Bosch
1996-10-12  0:00 ` Keith Thompson
1996-10-13  0:00 parker
1996-10-13  0:00 ` Robert Dewar
1996-10-14  0:00 ` Robert A Duff
1997-12-19  0:00 random number generation Mok-kong Shen
1998-01-02  0:00 ` Mok-kong Shen
1998-01-02  0:00   ` Robert Dewar
2003-09-26  7:00 Andrew
2003-09-26  7:35 ` tmoran
2003-09-26 17:58   ` Andrew
2003-09-26 19:25   ` Andrew
2003-09-26 19:35     ` chris
2003-09-26 21:44     ` tmoran
2003-09-27  1:40     ` Robert I. Eachus
2003-09-27  4:48       ` Andrew
2003-09-26  7:14 christoph.grein
2010-07-13 12:45 Random " tonyg
2010-07-13 12:50 ` Jacob Sparre Andersen
2010-07-13 12:58 ` Dmitry A. Kazakov
2010-07-13 13:17 ` Thomas Løcke
2010-07-13 16:07 ` Jeffrey R. Carter
2010-07-13 20:33   ` John B. Matthews
2010-07-13 23:02     ` Jeffrey R. Carter
2010-07-14  4:42       ` John B. Matthews
2010-07-15 19:01         ` tonyg
2010-12-30 10:43 Mart van de Wege
2010-12-30 10:54 ` Thomas Løcke
2010-12-30 12:11   ` Mart van de Wege
2010-12-30 11:34 ` Niklas Holsti
2010-12-30 11:53   ` Georg Bauhaus
2010-12-30 12:25     ` Mart van de Wege
2010-12-30 15:29       ` Georg Bauhaus
2010-12-30 15:37         ` Mart van de Wege
2010-12-30 11:51 ` Brian Drummond
2010-12-30 12:16   ` Mart van de Wege
2010-12-30 13:04 ` Dmitry A. Kazakov
2010-12-30 13:22   ` Niklas Holsti
2010-12-30 13:39     ` Dmitry A. Kazakov
2010-12-30 13:30   ` Mart van de Wege
2010-12-31  3:14 ` Gene
replies disabled

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