Skip to content

Commit 73917f8

Browse files
authored
Backport "Use sampler-based Random API (#206)" (#305)
This also backports the changes in PR #270, #278.
1 parent c372941 commit 73917f8

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
33
version = "0.8.5"
44

55
[deps]
6+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
67
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
78

89
[compat]
10+
StableRNGs = "1"
911
julia = "1"
1012

1113
[extras]
14+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
1215
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1316

1417
[targets]
15-
test = ["Test"]
18+
test = ["StableRNGs", "Test"]

src/FixedPointNumbers.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
77
big, rationalize, float, trunc, round, floor, ceil, bswap, clamp,
88
div, fld, rem, mod, mod1, fld1, min, max, minmax,
99
signed, unsigned, copysign, flipsign, signbit,
10-
rand, length
10+
length
1111

1212
import Statistics # for _mean_promote
13+
import Random: Random, AbstractRNG, SamplerType, rand!
1314

1415
using Base.Checked: checked_add, checked_sub, checked_div
1516

@@ -326,8 +327,15 @@ scaledual(::Type{Tdual}, x::AbstractArray{T}) where {Tdual, T <: FixedPoint} =
326327
throw(ArgumentError("$X is $bitstring type representing $n values from $Xmin to $Xmax; cannot represent $x"))
327328
end
328329

329-
rand(::Type{T}) where {T <: FixedPoint} = reinterpret(T, rand(rawtype(T)))
330-
rand(::Type{T}, sz::Dims) where {T <: FixedPoint} = reinterpret(T, rand(rawtype(T), sz))
330+
function Random.rand(r::AbstractRNG, ::SamplerType{X}) where X <: FixedPoint
331+
X(rand(r, rawtype(X)), 0)
332+
end
333+
334+
function rand!(r::AbstractRNG, A::Array{X}, ::SamplerType{X}) where {T, X <: FixedPoint{T}}
335+
At = unsafe_wrap(Array, reinterpret(Ptr{T}, pointer(A)), size(A))
336+
Random.rand!(r, At, SamplerType{T}())
337+
A
338+
end
331339

332340
if VERSION >= v"1.1" # work around https://github.com/JuliaLang/julia/issues/34121
333341
include("precompile.jl")

src/precompile.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Random
2+
13
function _precompile_()
24
ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
35
normedtypes = (N0f8, N0f16) # precompiled Normed types

test/fixed.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FixedPointNumbers, Statistics, Test
1+
using FixedPointNumbers, Statistics, Random, StableRNGs, Test
22
using FixedPointNumbers: bitwidth
33

44
# issue #288
@@ -404,6 +404,8 @@ end
404404
@test ndims(a) == 2 && eltype(a) == F
405405
@test size(a) == (3,5)
406406
end
407+
@test !(rand(Q0f15) == rand(Q0f15) == rand(Q0f15)) # If this fails, we should suspect a bug.
408+
@test rand(StableRNG(1234), Q0f7) === 0.531Q0f7
407409
end
408410

409411
@testset "floatmin" begin

test/normed.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FixedPointNumbers, Statistics, Test
1+
using FixedPointNumbers, Statistics, Random, StableRNGs, Test
22
using FixedPointNumbers: bitwidth
33

44
# issue #288
@@ -595,6 +595,8 @@ end
595595
@test ndims(a) == 2 && eltype(a) == T
596596
@test size(a) == (3,5)
597597
end
598+
@test !(rand(N0f16) == rand(N0f16) == rand(N0f16)) # If this fails, we should suspect a bug.
599+
@test rand(StableRNG(1234), N0f8) === 0.267N0f8
598600
end
599601

600602
@testset "Overflow with Float16" begin

0 commit comments

Comments
 (0)