Skip to content

Commit

Permalink
Broadcast AbstractRNG like a scalar (#33213)
Browse files Browse the repository at this point in the history
* Broadcast AbstractRNG like a scalar

* Add PR link to NEWS
  • Loading branch information
nickrobinson251 authored and mbauman committed Sep 23, 2019
1 parent cc6ae96 commit 187ee96
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ Standard library changes

* `dot` now admits a 3-argument method `dot(x, A, y)` to compute generalized dot products `dot(x, A*y)`, but without computing and storing the intermediate result `A*y` ([#32739]).

#### SparseArrays
#### Random


* `AbstractRNG`s now behave like scalars when used in broadcasting ([#33213]).

* Products involving sparse arrays now allow more general sparse `eltype`s, such as `StaticArrays` ([#33205])

#### Dates

#### SparseArrays

#### Dates

#### Statistics

Expand Down
2 changes: 2 additions & 0 deletions stdlib/Random/src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Supertype for random number generators such as [`MersenneTwister`](@ref) and [`R
"""
abstract type AbstractRNG end

Base.broadcastable(x::AbstractRNG) = Ref(x)

gentype(::Type{X}) where {X} = eltype(X)
gentype(x) = gentype(typeof(x))

Expand Down
4 changes: 4 additions & 0 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,7 @@ end
@test Sampler(GLOBAL_RNG, 2:4, Val(1)) isa SamplerRangeFast
@test Sampler(GLOBAL_RNG, 2:4, Val(Inf)) isa SamplerRangeFast
end

@testset "RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice)
@test length.(rand.(T(), 1:3)) == 1:3
end

0 comments on commit 187ee96

Please sign in to comment.