Topic: Constructing uniform_int_distribution and uniform_real_distribution


Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Mon, 5 Nov 2007 18:02:16 GMT
Raw View
Hi,

<disclaimer>
I understand from a previous post of Walter E Brown's that the whole
[rand] clause is the result of a tremendous amount of work and I
shamefully admit that I'm not in the condition to read all those papers.
It's quite probable that the issue I am going to expose has been
discussed before, in that case I apologize in advance for the noise.
</disclaimer>

The uniform_int_distribution and uniform_real_distribution have one
constructor each that initialize the underlying range:

explicit uniform_int_distribution(IntType a = 0, IntType b =
numeric_limits<IntType>::max());

explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);

The presence of the default values makes this line:

  uniform_int_distribution uid(10);

equivalent to this line:

  uniform_int_distribution uid(10, numeric_limits<int>::max());

however, probably due the previous exposure to several other
non-enlightened random number libraries, I cannot resist the urge to
interpret the former line as equivalent to:

  uniform_int_distribution uid(0, 10);

This urge and the fact that range [0,10] is bound to occur in practice
more frequently than [10,max] has created in my mind the idea that the
proposed constructor is error-prone and can be improved. Here are two
ways for achieving that:

- (first way) declare three constructors:

  uniform_int_distribution(); // a = 0, b = numeric_limits<IntType>::max()

  explicit uniform_int_distribution(IntType b); // a = 0, b as specified

  uniform_int_distribution(IntType a, IntType b); // both a and b as
specified

- (second way) declare two constructors:

  uniform_int_distribution(); // a = 0, b = numeric_limits<IntType>::max()

  uniform_int_distribution(IntType a, IntType b); // both a and b as
specified

The first way will satisfy my urge in a way that I find more reasonable,
but I admit that my reasons may sound arbitrary to a lot of people.
Moreover, it would break symmetry will all other distributions, where
arguments are always matched positionally.

The second way would simply disallow the specification of an incomplete
range. While I don't see it as a perfect solution, it's better than
nothing: at least my error-prone attempt to use only one argument would
be caught at compile-time and there would be no loss of symmetry.

The same argument can be applied to uniform_real_distribution, of course!

Just my two eurocent,

Ganesh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: wb@fnal.gov (Walter E Brown)
Date: Tue, 6 Nov 2007 00:33:17 GMT
Raw View
On 2007-11-05 12:02 PM, Alberto Ganesh Barbati wrote:
 > Hi,
 >
 > <disclaimer>
 > I understand from a previous post of Walter E Brown's that the whole
 > [rand] clause is the result of a tremendous amount of work and I
 > shamefully admit that I'm not in the condition to read all those papers.
 > It's quite probable that the issue I am going to expose has been
 > discussed before, in that case I apologize in advance for the noise.
 > </disclaimer>
 >
 > The uniform_int_distribution and uniform_real_distribution have one
 > constructor each that initialize the underlying range:
 >
 > explicit uniform_int_distribution(IntType a = 0, IntType b =
 > numeric_limits<IntType>::max());
 >
 > explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0);
 >
 > The presence of the default values makes this line:
 >
 >   uniform_int_distribution uid(10);
 >
 > equivalent to this line:
 >
 >   uniform_int_distribution uid(10, numeric_limits<int>::max());
 >
 > however, probably due the previous exposure to several other
 > non-enlightened random number libraries, I cannot resist the urge to
 > interpret the former line as equivalent to:
 >
 >   uniform_int_distribution uid(0, 10);
 >
 > This urge and the fact that range [0,10] is bound to occur in practice
 > more frequently than [10,max] has created in my mind the idea that the
 > proposed constructor is error-prone and can be improved. Here are two
 > ways for achieving that:
 >
 > - (first way) declare three constructors:
 >
 >   uniform_int_distribution(); // a = 0, b =
numeric_limits<IntType>::max()
 >
 >   explicit uniform_int_distribution(IntType b); // a = 0, b as specified
 >
 >   uniform_int_distribution(IntType a, IntType b); // both a and b as
 > specified
 >
 > - (second way) declare two constructors:
 >
 >   uniform_int_distribution(); // a = 0, b =
numeric_limits<IntType>::max()
 >
 >   uniform_int_distribution(IntType a, IntType b); // both a and b as
 > specified
 >
 > The first way will satisfy my urge in a way that I find more reasonable,
 > but I admit that my reasons may sound arbitrary to a lot of people.
 > Moreover, it would break symmetry will all other distributions, where
 > arguments are always matched positionally.
 >
 > The second way would simply disallow the specification of an incomplete
 > range. While I don't see it as a perfect solution, it's better than
 > nothing: at least my error-prone attempt to use only one argument would
 > be caught at compile-time and there would be no loss of symmetry.
 >
 > The same argument can be applied to uniform_real_distribution, of course!

While we are sympathetic to your view, it may be no longer feasible to
change the interface to these distributions to be incompatible with the
equivalent distributions in TR1.  If you feel that making such a change
would be a significant enhancement in the usefulness of these
distributions, you are welcome to submit a defect report to have the
Library Working Group make this determination.

-- WEB

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]