Skip to content

Commit 41028c0

Browse files
authored
added rand for TypedEndpointsInterval (#116)
* added rand for TypedEndpointsInterval * disallow rand(::TypedInterval{:closed, :open})
1 parent 3054532 commit 41028c0

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/interval.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ end
222222

223223
# random sampling from interval
224224
Random.gentype(::Type{Interval{L,R,T}}) where {L,R,T} = float(T)
225-
function Random.rand(rng::AbstractRNG, i::Random.SamplerTrivial{ClosedInterval{T}}) where {T<:Real}
225+
function Random.rand(rng::AbstractRNG, i::Random.SamplerTrivial{<:TypedEndpointsInterval{:closed, :closed, T}}) where T<:Real
226226
_i = i[]
227227
isempty(_i) && throw(ArgumentError("The interval should be non-empty."))
228228
a,b = endpoints(_i)
229-
t = rand(rng, float(T))
229+
t = rand(rng, float(T)) # technically this samples from [0, 1), but we still allow it with TypedEndpointsInterval{:closed, :closed} for convenience
230230
return clamp(t*a+(1-t)*b, _i)
231231
end
232232

test/runtests.jl

+5
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,11 @@ struct IncompleteInterval <: AbstractInterval{Int} end
769769
rand(i4,10) i4
770770
rand(i5,10) i5
771771
end
772+
773+
# special test to catch issue mentioned at the end of https://github.com/JuliaApproximation/DomainSets.jl/pull/112
774+
struct RandTestUnitInterval <: TypedEndpointsInterval{:closed, :closed, Float64} end
775+
IntervalSets.endpoints(::RandTestUnitInterval) = (-1.0, 1.0)
776+
@test rand(RandTestUnitInterval()) in -1.0..1.0
772777
end
773778

774779
@testset "IteratorSize" begin

0 commit comments

Comments
 (0)