Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rand(1..2) #104

Merged
merged 6 commits into from
May 31, 2022
Merged

Conversation

hyrodium
Copy link
Collaborator

This PR fixes #95.

julia> using IntervalSets, Dates

julia> rand(1..2)  # Sampling from uniform distion on 1..2
1.525710842368245

julia> rand(1..2, 3)  # Get three samples
3-element Vector{Float64}:
 1.0470183699383326
 1.406808529012634
 1.2230905556202805

julia> rand(42..42)  # Accepts closed inerval with zero width.
42.0

julia> rand(Interval{:open,:closed}(1,3))  # Only ClosedInterval is supported

ERROR: ArgumentError: Sampler for this object is not defined
Stacktrace:
 [1] Random.Sampler(#unused#::Type{Random.TaskLocalRNG}, sp::Random.SamplerTrivial{Interval{:open, :closed, Int64}, Float64}, #unused#::Val{1})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:146
 [2] Random.Sampler(rng::Random.TaskLocalRNG, x::Random.SamplerTrivial{Interval{:open, :closed, Int64}, Float64}, r::Val{1})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:140
 [3] rand(rng::Random.TaskLocalRNG, X::Random.SamplerTrivial{Interval{:open, :closed, Int64}, Float64}) (repeats 2 times)
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:254
 [4] rand(X::Interval{:open, :closed, Int64})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:259
 [5] top-level scope
   @ REPL[5]:1

julia> rand(42..41)  # Error on empty interval
ERROR: ArgumentError: The interval should be non-empty.
Stacktrace:
 [1] rand(rng::Random.TaskLocalRNG, i::Random.SamplerTrivial{ClosedInterval{Int64}, Float64})
   @ IntervalSets ~/.julia/dev/IntervalSets/src/interval.jl:236
 [2] rand
   @ ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:254 [inlined]
 [3] rand(X::ClosedInterval{Int64})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:259
 [4] top-level scope
   @ REPL[6]:1

julia> rand(Date(2022,04,10)..Date(2022,04,12))  # Only ClosedInterval{<:Real} is supported
ERROR: MethodError: no method matching AbstractFloat(::Type{Date})
Closest candidates are:
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at ~/julia/julia-1.7.1/share/julia/base/char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at ~/julia/julia-1.7.1/share/julia/base/twiceprecision.jl:255
  (::Type{T})(::Complex) where T<:Real at ~/julia/julia-1.7.1/share/julia/base/complex.jl:44
  ...
Stacktrace:
 [1] float(x::Type)
   @ Base ./float.jl:269
 [2] gentype(#unused#::Type{ClosedInterval{Date}})
   @ IntervalSets ~/.julia/dev/IntervalSets/src/interval.jl:233
 [3] Random.SamplerTrivial(x::ClosedInterval{Date})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:180
 [4] Random.Sampler(#unused#::Type{Random.TaskLocalRNG}, x::ClosedInterval{Date}, #unused#::Val{1})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:182
 [5] Random.Sampler(rng::Random.TaskLocalRNG, x::ClosedInterval{Date}, r::Val{1})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:140
 [6] rand(rng::Random.TaskLocalRNG, X::ClosedInterval{Date})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:254
 [7] rand(X::ClosedInterval{Date})
   @ Random ~/julia/julia-1.7.1/share/julia/stdlib/v1.7/Random/src/Random.jl:259
 [8] top-level scope
   @ REPL[7]:1

@codecov
Copy link

codecov bot commented Apr 10, 2022

Codecov Report

Merging #104 (d026437) into master (37759b8) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #104      +/-   ##
==========================================
+ Coverage   99.20%   99.22%   +0.02%     
==========================================
  Files           3        3              
  Lines         251      258       +7     
==========================================
+ Hits          249      256       +7     
  Misses          2        2              
Impacted Files Coverage Δ
src/IntervalSets.jl 99.08% <ø> (ø)
src/interval.jl 99.17% <100.00%> (+0.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 37759b8...d026437. Read the comment docs.

isempty(_i) && throw(ArgumentError("The interval should be non-empty."))
a,b = endpoints(_i)
t = rand(rng, float(T))
return clamp(t*a+(1-t)*b, _i)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this clamp, the rand may reduce non-unique samples.

julia> a = rand()*10e100
4.4188530133566296e100

julia> unique([rand(a..a) for _ in 1:10000])
3-element Vector{Float64}:
 4.4188530133566296e100
 4.418853013356629e100
 4.41885301335663e100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for rand(1..2)
1 participant