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

[new feature] Random.eachrand #35290

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion stdlib/Random/src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export rand!, randn!,
shuffle, shuffle!,
randperm, randperm!,
randcycle, randcycle!,
AbstractRNG, MersenneTwister, RandomDevice
AbstractRNG, MersenneTwister, RandomDevice,
eachrand

## general definitions

Expand Down Expand Up @@ -258,6 +259,22 @@ rand(rng::AbstractRNG=default_rng(), ::Type{X}=Float64) where {X} = rand(rng, Sa
rand(X) = rand(default_rng(), X)
rand(::Type{X}) where {X} = rand(default_rng(), X)


struct IterableRNG{X,R}
rng :: AbstractRNG
end

Base.iterate(ibl::IterableRNG{X,R}, itr=ibl.rng) where {X,R} = (rand(itr,R), itr)
Base.IteratorSize(::Type{<:IterableRNG}) = Base.IsInfinite()
Base.IteratorEltype(::Type{<:IterableRNG}) = Base.HasEltype()
Base.eltype(::Type{IterableRNG{X,R}}) where {X,R} = X

eachrand(rng::AbstractRNG=default_rng(), ::Type{X}=Float64) where {X} = IterableRNG{X,X}(rng)
eachrand(::Type{X}) where {X} = IterableRNG{X,X}(default_rng())
eachrand(rng::AbstractRNG, xs::AbstractRange{X}) where {X} = IterableRNG{X,xs}(rng)
eachrand(xs::AbstractRange{X}) where {X} = IterableRNG{X,xs}(default_rng())


#### arrays

rand!(A::AbstractArray{T}, X) where {T} = rand!(default_rng(), A, X)
Expand Down
11 changes: 11 additions & 0 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using Random.DSFMT
using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I

import Future # randjump
import Base.Iterators # take

@testset "Issue #6573" begin
Random.seed!(0)
Expand Down Expand Up @@ -778,3 +779,13 @@ end
@testset "RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice)
@test length.(rand.(T(), 1:3)) == 1:3
end

@testset "eachrand" begin
s = (mt = MersenneTwister(1234); [rand(mt, 0:255) for _ in 1:3])
@test collect(Iterators.take(eachrand(MersenneTwister(1234), UInt8),3)) == s
@test collect(Iterators.take(eachrand(MersenneTwister(1234), 0:255),3)) == s
@test collect(Iterators.take(eachrand(MersenneTwister(1234), 0:255),3)) == s
@test (Random.seed!(1234); collect(Iterators.take(eachrand(UInt8), 3))) == s
@test (Random.seed!(1234); collect(Iterators.take(eachrand(0:255), 3))) == s
@test (Random.seed!(1234); collect(Iterators.take(eachrand(), 3))) ≈ (Random.seed!(1234); [rand() for _ in 1:3])
end