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

Fix #182 #184

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/InfiniteArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import LinearAlgebra: AdjOrTrans, HermOrSym, diag, norm, norm1, norm2, normp

import LazyArrays: AbstractPaddedLayout

import Random: default_rng
import Random: default_rng, seed!

export ∞, ℵ₀, Hcat, Vcat, Zeros, Ones, Fill, Eye, BroadcastArray, cache
import Base: unitrange, oneto
Expand Down
23 changes: 21 additions & 2 deletions src/infrand.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
_dist_type(dist) = typeof(dist)
_dist_type(dist::Type{T}) where {T} = Type{T} # avoid DataType for typeof(Float64), since DataType means rand(seq.rng, seq.dist) is inferred as Any
function _reset_seed!(rng)
new_seed = rand(rng, UInt32)
return seed!(rng, new_seed)
end

"""
InfRandVector([rng=default_rng()], [dist=Float64])

Represents a random infinite sequence. The random number generator can be specified
by the first argument `rng`, which defaults to `Random.default_rng()`, and the distribution
to generate from can be specified using the `dist` argument, which defaults to `Float64`.

!!! note "Mutation of rng"

When an InfRandVector is constructed, the `rng` (or, if
no `rng` is provided, the global `rng`) gets reseeded
using `Random.seed!(rng, rand(rng, UInt32))`.

# Examples
```julia-repl
julia> using InfiniteArrays

Expand Down Expand Up @@ -41,14 +55,19 @@ mutable struct InfRandVector{T,D,RNG} <: AbstractCachedVector{T}
const data::Vector{T}
datasize::Int
end

function InfRandVector(rng=default_rng(), dist=Float64)
T = typeof(rand(copy(rng), dist))
_rng = copy(rng)
return InfRandVector{T,typeof(dist),typeof(_rng)}(_rng, dist, T[], 0)
_reset_seed!(rng)
return InfRandVector{T,_dist_type(dist),typeof(_rng)}(_rng, dist, T[], 0)
end

Base.size(::InfRandVector) = (ℵ₀,)
Base.axes(::InfRandVector) = (1:ℵ₀,)
Base.length(::InfRandVector) = ℵ₀

@inline _single_rand(seq::InfRandVector) = rand(seq.rng, seq.dist)
function resizedata!(seq::InfRandVector, inds)
newlen = maximum(inds)
curlen = length(seq.data)
Expand All @@ -57,7 +76,7 @@ function resizedata!(seq::InfRandVector, inds)
# rand!(seq.rng, view(seq.data, curlen+1:newlen), seq.dist)
# ^ rand() is not actually sequential.. rand(Random.seed!(123), 1000) ≠ (rng = Random.seed!(123); [rand(rng) for _ in 1:1000])
for i in (curlen+1):newlen
seq.data[i] = rand(seq.rng, seq.dist)
seq.data[i] = _single_rand(seq)
end
seq.datasize = newlen
return seq
Expand Down
15 changes: 15 additions & 0 deletions test/test_infrand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
@test size(seq) == (ℵ₀,)
@test length(seq) == ℵ₀
@test axes(seq) == (1:ℵ₀,)
@inferred InfiniteArrays._single_rand(seq)
end

@testset "Providing an RNG and a distribution" begin
rng = MersenneTwister(123)
seq = InfiniteArrays.InfRandVector(rng, Float16)
rng2 = MersenneTwister(123)
@test seq[1:10000] == [rand(rng2, Float16) for _ in 1:10000]
@inferred InfiniteArrays._single_rand(seq)
end

@testset "Distributions.jl" begin
Expand All @@ -28,5 +30,18 @@
seq = InfiniteArrays.InfRandVector(rng, dist)
rng2 = Xoshiro(5)
@test seq[1:100] == [0.3 + 1.7randn(rng2) for _ in 1:100]

@test InfiniteArrays._dist_type(dist) == Normal{Float64}
@test InfiniteArrays._dist_type(Float64) == Type{Float64}
@inferred InfiniteArrays._single_rand(seq)
end

@testset "Issue #182" begin
kp = InfiniteArrays.InfRandVector()[1:1000]
kp2 = InfiniteArrays.InfRandVector()[1:1000]
kp3 = InfiniteArrays.InfRandVector()[1:1000]
@test kp ≠ kp2
@test kp2 ≠ kp3
@test kp ≠ kp3
end
end
Loading