From b8a6245cc4505e53a31c5b0e1b9bc5e8d834d93a Mon Sep 17 00:00:00 2001 From: amrods <11388389+amrods@users.noreply.github.com> Date: Mon, 15 Apr 2019 02:48:13 +1000 Subject: [PATCH] Fixes Issue #482 (issue with multidimensional sampling) (#483) --- src/sampling.jl | 2 +- test/sampling.jl | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/sampling.jl b/src/sampling.jl index 880d3feaaa8f32..1e0d47970cbe08 100644 --- a/src/sampling.jl +++ b/src/sampling.jl @@ -390,7 +390,7 @@ Optionally specify a random number generator `rng` as the first argument """ function sample(rng::AbstractRNG, a::AbstractArray{T}, dims::Dims; replace::Bool=true, ordered::Bool=false) where T - sample!(rng, a, Array{T}(undef, dims), rng; replace=replace, ordered=ordered) + sample!(rng, a, Array{T}(undef, dims); replace=replace, ordered=ordered) end sample(a::AbstractArray, dims::Dims; replace::Bool=true, ordered::Bool=false) = sample(Random.GLOBAL_RNG, a, dims; replace=replace, ordered=ordered) diff --git a/test/sampling.jl b/test/sampling.jl index e48e22a4b29d79..4f307d8708ad58 100644 --- a/test/sampling.jl +++ b/test/sampling.jl @@ -170,6 +170,13 @@ check_sample_norep(a, (3, 12), 0; ordered=false) a = sample(3:12, 5; replace=false, ordered=true) check_sample_norep(a, (3, 12), 0; ordered=true) +# tests of multidimensional sampling + +a = sample(3:12, (2, 2); replace=false) +check_sample_norep(a, (3, 12), 0; ordered=false) + +@test sample(1:1, (2, 2); replace=true) == ones(Int, 2, 2) + # test of weighted sampling without replacement a = [1:10;] wv = Weights([zeros(6); 1:4])