jo*********@gmail.com wrote:
How do I get uniform random numbers in the log domain? I need a random
number in the log domain between -infinity and -0.69314718, which is
equivalent to a range from 0 to 0.5.
I usually use this code to get a uniform random number between, say, 0
and 0.5:
double r = lowest + range * rand() / (RAND_MAX + 1.0);
But it won't work in the log domain when my lowest is -inf, and the
range is also infinite? I have a costly numberical procedure and
cannot afford to exponentiate the numbers that are coming in (-inf ->
0, -0.69314718 -0.5). So it would be good to get a random number
directly in the log domain.
a) Exponentiating uniformly distributed random numbers in (0,0.5) will not
yield uniformly distributed random numbers between (-inf,-0.69...).
b) You cannot be serious about -inf; or you cannot be serious about being
uniformly distributed. There is no meaningfull uniform distribution on
(-inf,-0.69...). You have to truncate the domain somewhere.
c) ( -0.7 - std::rand() ) is uniformly distributed in a domain pretty close
to the one you want (since you have to truncate anyhow). You could also
rescale the std::rand() component by a constant factor.
d) If you need to control the quality of the random number generator, or if
you prefer a different distribution, have a look into tr1.
Best
Kai-Uwe Bux