Boost logo

Boost Users :

Subject: Re: [Boost-users] [random] infinite loop in uniform_real_distribution
From: Brian Budge (brian.budge_at_[hidden])
Date: 2013-11-25 17:22:30


On Mon, Nov 25, 2013 at 1:27 PM, Jérémy Coulon <jeremy.coulon_at_[hidden]> wrote:
> Le 25/11/2013 22:03, Brian Budge a écrit :
>
>> It seems to me that it's not really just "large" that is the issue,
>> but only if you have a range larger than
>> std::numeric_limits<float>::max(). Ideally, you'd not really want to
>> be messing with too very many numbers that are within many orders of
>> magnitude of the max float value, as your precision will be awful.
>> Are you just edge-case testing, or do you have a real reason to
>> generate uniform distributions over this range?
>>
>> Brian
>
> The documentation doesn't say anything about the input range of the
> distribution.
> So I expect it to work with *any* range.

Fair enough :) I'd say that you are right that it should be documented.

> As for the real reason, I need to unit-test a function of my own code that
> takes a float as an argument.
> I want to test that my function behaves correctly for any given float.
> Using a few thousands of randomly generated floats and calling my function
> with that seems a good idea.
>

Sure. I suppose you have other test cases for inf, NaN, and denorm
values? How will you know it behaves correctly for the random input
(unless of course, you have another implementation that can run in
lockstep to verify correctness?). Sorry, I'm sure you're not here
looking for criticism of your testing methods :)

You can achieve what you want by generating a random number on [-1, 1]
and multiplying it by the max float. I don't think that there is a
*general* way to arrive at a uniform distribution of a range greater
than max float by first generating a random number on [0, 1), so it's
unlikely that the library can be "fixed" to achieve what you want.

Another method would be to use double precision generation on the max
float range, and then convert to float.

>
> Jeremy

  Brian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net