Skip to content

FR: Random sampler for Iterators.ProductIterator #43266

@fonsp

Description

@fonsp

Feature

I love Iterators.product, and it would be great if I could call rand on it directly:

julia> Iterators.product([1,2],[3,4,5]) |> rand
(1, 5)

Currently, this does not work, because no sampler is defined:

Stacktrace
julia> Iterators.product([1,2],[3,4,5]) |> rand

ERROR: ArgumentError: Sampler for this object is not defined
Stacktrace:
 [1] Random.Sampler(#unused#::Type{Random.MersenneTwister}, sp::Random.SamplerTrivial{Base.Iterators.ProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, Tuple{Int64, Int64}}, #unused#::Val{1})
   @ Random /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/Random/src/Random.jl:145
 [2] Random.Sampler(rng::Random.MersenneTwister, x::Random.SamplerTrivial{Base.Iterators.ProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, Tuple{Int64, Int64}}, r::Val{1})
   @ Random /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/Random/src/Random.jl:139
 [3] rand(rng::Random.MersenneTwister, X::Random.SamplerTrivial{Base.Iterators.ProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, Tuple{Int64, Int64}}) (repeats 2 times)
   @ Random /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/Random/src/Random.jl:253
 [4] rand(X::Base.Iterators.ProductIterator{Tuple{Vector{Int64}, Vector{Int64}}})
   @ Random /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/Random/src/Random.jl:258

Implementation

A simple implementation would be this:

function Random.rand(
    rng::Random.AbstractRNG,
    iterator::Random.SamplerTrivial{Base.Iterators.ProductIterator{T}},
) where {T}
    r(x) = rand(rng, x)
    r.(iterator[].iterators)
end

Would this PR be welcome? Where in the codebase should the implementation go? Should it be a method of Random.Sampler?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requestsgood first issueIndicates a good issue for first-time contributors to JuliarandomnessRandom number generation and the Random stdlib

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions