comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: Random Number Generation
Date: 1996/10/01
Date: 1996-10-01T00:00:00+00:00	[thread overview]
Message-ID: <DyKpEz.FnB.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 52p14c$h7e@newton.cc.rl.ac.uk


Dr J Parker (jp3@rl.ac.uk) wrote:
: Tucker Taft suggests: 

: "In any case, Ada.Numerics.Float_Random is the easiest one to adapt to your
: use, since it always generates values between 0.0 and 1.0.  You
: can then multiply that value by an appropriate integer or float
: and add the low bound to produce the desired dynamic range.

:   declare
:     Result : Integer range Low_Bound .. High_Bound;
:     Range_Length : constant Integer := High_Bound - Low_Bound + 1;
:   begin
:     Result := (Integer(Float(Range_Length) *
:       Ada.Numerics.Float_Random.Random(Gen)) mod Range_Length) + Low_Bound;
:     ...
: "


: Careful, there are some limits in which this fails. Simple example:
: Suppose you have a high quality random number generator R1 in the range
: 0 .. 127
: and you want to turn it into a high quality random number generator R2
: in the range 0 .. 82.
: Only the rejection method works: you must throw out at least
: 45 of the values returned by R1.  eg, if R1 returns a number in the range
: 83..127, then reject it and keep calling R1 until you get
: a number in the range 0 .. 82.  You'll get a stream of high quality random
: numbers in the range 0 .. 82.  In general, you reject just enough to make
: the number of elements (think bins) in range of R1 an integer multiple of
: the number of elements in the range of R2 (you reject never more than 50% of
: the number of elements in range of R1).  Then you
: can use "*"'s, etc to transform R1 into R2. Floating point doesn't
: change the problem.

I don't believe my suggestion suffers from the problem you fear.

The standard Float random number generator generates values uniformly
distributed from 0.0 to 1.0.  By multiplying these values by the number
of distinct integers wanted, and then (carefully) "pushing" them to a
neighboring exact integral value, you can get a uniform distribution
across any desired sequence of integers, provided the number of distinct
integers is significantly less than the period of the underlying generator.

The only trickiness above is that Ada rounds on conversion from float
to integer, so after multiplying by N and rounding to integer, you end 
up with 0 and N each occuring half as often as the values 1..N-1.  The
"mod" folds the N and 0 together, providing the desired uniform distribution
from 0 .. N-1, and then adding the low bound gives the desired low .. high
distribution.  If N is a very large number, on the order of the
underlying period of the Float random number generator, then clearly
the "uniformity" of the generator will not be as good, but provided
N << period, there is no need to use "rejection" to produce reasonable
uniformity.

-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Cambridge, MA  USA




  reply	other threads:[~1996-10-01  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   ` Nigel J. Tracey
1996-10-02  0:00   ` Robert I. Eachus
1996-10-03  0:00   ` Nigel J. Tracey
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 [this message]
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