Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#7020 closed Bugs (invalid)

uniform_01 improvement

Reported by: agribov_no_s_p_a_m@… Owned by: No-Maintainer
Milestone: To Be Determined Component: random
Version: Boost 1.49.0 Severity: Optimization
Keywords: uniform_01 Cc:

Description

The current code to simulate uniform random number in interval [0;1] contains verification to skip 1.

if (result < result_type(1))

return result;

It means that random generator can generate 0 but not 1. This is used in some other code, for example, generate Exponential random number:

return -result_type(1) /

_lambda * log(result_type(1)-uniform_01<RealType>()(eng));

Meaning (1 - random number) will never be 0.

If eng() returns integer number, then, with some limitations for result_type and generated range of random numbers (result_type has to be double for generated 32 bit random numbers):

result_type factor = result_type(1) / (result_type((eng.max)()-(eng.min)()) + result_type(1));

return (result_type(eng() - (eng.min)()) + result_type(0.5)) * factor;

In this case, the result will never contain 0 or 1 and will be unbiased. No more checking for 0 or 1 is necessary.

Change History (2)

comment:1 by Steven Watanabe, 10 years ago

Resolution: invalid
Status: newclosed

The check for 1 is *already* unnecessary for 32-bit integers and double. It is needed when the mantissa has fewer bits than the integer, because values close to 1 can be rounded to 1.

comment:2 by anonymous, 10 years ago

I see, however it would be better to simulate a random number not including 0 (and 1). For example, this simplify code to generate random numbers from exponential distribution.

Note: See TracTickets for help on using tickets.