@@ -22,17 +22,26 @@ Sampler(::Type{RNG}, ::Type{T}, n::Repetition) where {RNG<:AbstractRNG,T<:Abstra
22
22
# generic random generation function which can be used by RNG implementors
23
23
# it is not defined as a fallback rand method as this could create ambiguities
24
24
25
- rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen01{Float16}} ) =
26
- Float16 (reinterpret (Float32,
27
- (rand (r, UInt10 (UInt32)) << 13 ) | 0x3f800000 ) - 1 )
25
+ function rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen01{Float16}} )
26
+ # bias correction below is equivalent to adding eps(Float16)/2, see #33222
27
+ z = reinterpret (Float32, (rand (r, UInt10 (UInt32)) << 13 ) | 0x3f800000 )
28
+ Float16 (z - prevfloat (Float16 (1 )))
29
+ end
28
30
29
- rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen01{Float32}} ) =
30
- reinterpret (Float32, rand (r, UInt23 ()) | 0x3f800000 ) - 1
31
+ function rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen01{Float32}} )
32
+ # bias correction below is equivalent to adding eps(Float32)/2, see #33222
33
+ reinterpret (Float32, rand (r, UInt23 ()) | 0x3f800000 ) - prevfloat (1f0 )
34
+ end
31
35
32
- rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen12_64} ) =
36
+ function rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen12_64} )
37
+ # NOTE this has a small bias of eps()/2, see discussion in #33222
33
38
reinterpret (Float64, 0x3ff0000000000000 | rand (r, UInt52 ()))
39
+ end
34
40
35
- rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen01_64} ) = rand (r, CloseOpen12 ()) - 1.0
41
+ function rand (r:: AbstractRNG , :: SamplerTrivial{CloseOpen01_64} )
42
+ # bias correction below is equivalent to adding eps()/2, see #33222
43
+ rand (r, CloseOpen12 ()) - prevfloat (1.0 )
44
+ end
36
45
37
46
# ### BigFloat
38
47
0 commit comments