See this discussion if you want the 1 instead:
I want a random Float64 in the interval (0,1].
I understand rand() returns a number in [0,1).
Should I just use 1-rand() ?
As for the reason: various conventions exist, this is a common one. It is somewhat convenient: the actual value is generated in [1, 2)
, then adjusted. You may be interested in the source, eg
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01{Float16}}) =
Float16(reinterpret(Float32,
(rand(r, UInt10(UInt32)) << 13) | 0x3f800000) - 1)
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01{Float32}}) =
reinterpret(Float32, rand(r, UInt23()) | 0x3f800000) - 1
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen12_64}) =
reinterpret(Float64, 0x3ff0000000000000 | rand(r, UInt52()))
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01_64}) = rand(r, CloseOpen12()) - 1.0
#### BigFloat
const bits_in_Limb = sizeof(Limb) << 3
const Limb_high_bit = one(Limb) << (bits_in_Limb-1)
struct SamplerBigFloat{I<:FloatInterval{BigFloat}} <: Sampler{BigFloat}
prec::Int
nlimbs::Int
limbs::Vector{Limb}
3 Likes