c - Getting a random real number in a certain range using WELL512 -
i'm using well512 pseudorandom number generator function described in this paper. function returns random unsigned long
value.
how use return value produce random real number within range - float between 340.92491 , 859812.53198 inclusive.
the documentation c rand() function seems warn against using mod.
well, mathematically it's just:
min_value + (max_value - min_value) * (my_random() / (long double)ulong_max)
(assuming my_random() returns uniformly distributed number between 0 , ulong_max)
however, depending on exact values of min_value
, max_value
, , ulong_max
, floating point numbers more others.
each possible random unsigned long maps float formula. since number of distinct floating point numbers between min_value
, max_value
not ulong_max
, unsigned longs map same floating point number or floating point numbers have no unsigned long map them or both.
fixing make result uniform is... non-trivial, think. maybe better read can cite paper.
[edit]
or see answer question:
generating random floating-point values based on random bit stream
that answer depends on internals of ieee double
representation. not sure understand how works.
[edit 2]
ok understand how works. idea pick random floating point representation between min , max, , throw out probability inversely proportional scale represented exponent. because uniform distribution, numbers between (say) 1/2 , 1 need half between 1 , 2, number of floating point representations in ranges same.
i think make code more efficient first picking exponent on logarithmic scale -- say, using ffs
on randomly-chosen integer -- , picking mantissa @ random. hm...
Comments
Post a Comment