Skip to content

Commit 5862909

Browse files
rfourquetwaTeim
authored andcommitted
enable rand(::AbstractArray, [dims])
This is a rewrite of 6d329ce, 9d0282e and d9814ff (from JuliaLang#8309, which was reverted).
1 parent 4b9032a commit 5862909

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

base/random.jl

+10-4
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ function rand{T<:Integer, U<:Unsigned}(g::RandIntGen{T,U})
369369
end
370370

371371
rand{T<:Union(Signed,Unsigned,Bool,Char)}(r::UnitRange{T}) = rand(RandIntGen(r))
372-
rand{T}(r::Range{T}) = r[rand(1:(length(r)))]
372+
373+
# Randomly draw a sample from an AbstractArray r
374+
# (e.g. r is a range 0:2:8 or a vector [2, 3, 5, 7])
375+
rand(r::AbstractArray) = @inbounds return r[rand(1:length(r))]
373376

374377
function rand!(g::RandIntGen, A::AbstractArray)
375378
for i = 1 : length(A)
@@ -380,16 +383,19 @@ end
380383

381384
rand!{T<:Union(Signed,Unsigned,Bool,Char)}(r::UnitRange{T}, A::AbstractArray) = rand!(RandIntGen(r), A)
382385

383-
function rand!(r::Range, A::AbstractArray)
386+
rand!(r::Range, A::AbstractArray) = _rand!(r, A)
387+
388+
# TODO: this more general version is "disabled" until #8246 is resolved
389+
function _rand!(r::AbstractArray, A::AbstractArray)
384390
g = RandIntGen(1:(length(r)))
385391
for i = 1 : length(A)
386392
@inbounds A[i] = r[rand(g)]
387393
end
388394
return A
389395
end
390396

391-
rand{T}(r::Range{T}, dims::Dims) = rand!(r, Array(T, dims))
392-
rand(r::Range, dims::Int...) = rand(r, dims)
397+
rand{T}(r::AbstractArray{T}, dims::Dims) = _rand!(r, Array(T, dims))
398+
rand(r::AbstractArray, dims::Int...) = rand(r, dims)
393399

394400
## random BitArrays (AbstractRNG)
395401

doc/stdlib/base.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -4028,9 +4028,13 @@ A ``MersenneTwister`` RNG can generate random numbers of the following types: ``
40284028

40294029
Populate the array A with random values.
40304030

4031-
.. function:: rand(r, [dims...])
4031+
.. function:: rand(coll, [dims...])
40324032

4033-
Pick a random element or array of random elements from range ``r`` (for example, ``1:n`` or ``0:2:10``).
4033+
Pick a random element or array of random elements from the indexable collection ``coll`` (for example, ``1:n`` or ``['x','y','z']``).
4034+
4035+
.. function:: rand!(r, A)
4036+
4037+
Populate the array A with random values drawn uniformly from the range ``r``.
40344038

40354039
.. function:: randbool([rng], [dims...])
40364040

test/random.jl

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ rand!(MersenneTwister(0), A)
3131
@test A == [858542123778948672 5715075217119798169;
3232
8690327730555225005 8435109092665372532]
3333

34+
# rand from AbstractArray
35+
@test rand(0:3:1000) in 0:3:1000
36+
coll = Any[2, UInt128(128), big(619), "string", 'c']
37+
@test rand(coll) in coll
38+
@test issubset(rand(coll, 2, 3), coll)
39+
3440
# randn
3541
@test randn(MersenneTwister(42)) == -0.5560268761463861
3642
A = zeros(2, 2)

0 commit comments

Comments
 (0)