Skip to content

Commit 7f9501c

Browse files
Broadcast AbstractRNG like a scalar
1 parent 2d4f4d2 commit 7f9501c

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

NEWS.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ Standard library changes
4141

4242
* `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]).
4343

44-
#### SparseArrays
44+
#### Random
4545

46+
* `AbstractRNG`s now behave like scalars when used in broadcasting ([]).
4647

47-
#### Dates
48+
#### SparseArrays
4849

50+
#### Dates
4951

5052
#### Statistics
5153

stdlib/Random/src/Random.jl

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Supertype for random number generators such as [`MersenneTwister`](@ref) and [`R
4040
"""
4141
abstract type AbstractRNG end
4242

43+
Base.broadcastable(x::AbstractRNG) = Ref(x)
44+
4345
gentype(::Type{X}) where {X} = eltype(X)
4446
gentype(x) = gentype(typeof(x))
4547

stdlib/Random/test/runtests.jl

+4
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,7 @@ end
774774
@test Sampler(GLOBAL_RNG, 2:4, Val(1)) isa SamplerRangeFast
775775
@test Sampler(GLOBAL_RNG, 2:4, Val(Inf)) isa SamplerRangeFast
776776
end
777+
778+
@testset "RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice)
779+
@test length.(rand.(T(), 1:3) == 1:3
780+
end

0 commit comments

Comments
 (0)