Skip to content

Commit

Permalink
Add rand_ui52 for some generic functions in Base.Random.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunoru committed Nov 29, 2016
1 parent e9be24d commit e24994e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/common.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Base.Random: rand
import Base.Random: rand, rand_ui52

"""
```julia
Expand All @@ -11,11 +11,14 @@ abstract AbstractRNG{T<:Number} <: Base.Random.AbstractRNG

typealias BitTypes Union{Bool, Signed, Unsigned}

@inline function rand_ui52(rng::AbstractRNG)
rand(rng, UInt64) & Base.significand_mask(Float64)
end

# see https://github.com/sunoru/RNG.jl/issues/8
# TODO: find a better approach.
@inline function rand{T<:Union{UInt64, UInt128}}(rng::AbstractRNG{T}, ::Type{Float64}=Float64)
u = rand(rng, UInt64)
reinterpret(Float64, Base.exponent_one(Float64) | Base.significand_mask(Float64) & u) - 1.0
reinterpret(Float64, Base.exponent_one(Float64) | rand_ui52(rng)) - 1.0
end
@inline function rand{T<:Union{UInt8, UInt16, UInt32}}(rng::AbstractRNG{T}, ::Type{Float64}=Float64)
rand(rng, T) * exp2(-sizeof(T) << 3)
Expand Down
6 changes: 6 additions & 0 deletions test/generic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
r = PCG.PCGStateOneseq(123)
@test randn(r) == -0.4957408739873887
@test randn(r, Float64, 8) == [
-0.38659391873108484, -0.12391575881839223, 0.305709506865529, 1.0234147128314572,
1.1925044460767074, 0.8448640519165571, -0.2982275988025108, 0.9615482011555472
]

0 comments on commit e24994e

Please sign in to comment.