Skip to content

Commit

Permalink
enable rand(::AbstractArray, [dims])
Browse files Browse the repository at this point in the history
This is a rewrite of 6d329ce, 9d0282e and d9814ff (from #8309,
which was reverted).
  • Loading branch information
rfourquet committed Nov 18, 2014
1 parent aa1f53b commit 66aa1a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
14 changes: 10 additions & 4 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ function rand{T<:Integer, U<:Unsigned}(g::RandIntGen{T,U})
end

rand{T<:Union(Signed,Unsigned,Bool,Char)}(r::UnitRange{T}) = rand(RandIntGen(r))
rand{T}(r::Range{T}) = r[rand(1:(length(r)))]

# Randomly draw a sample from an AbstractArray r
# (e.g. r is a range 0:2:8 or a vector [2, 3, 5, 7])
rand(r::AbstractArray) = @inbounds return r[rand(1:length(r))]

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

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

function rand!(r::Range, A::AbstractArray)
rand!(r::Range, A::AbstractArray) = _rand!(r, A)

# TODO: this more general version is "disabled" until #8246 is resolved
function _rand!(r::AbstractArray, A::AbstractArray)
g = RandIntGen(1:(length(r)))
for i = 1 : length(A)
@inbounds A[i] = r[rand(g)]
end
return A
end

rand{T}(r::Range{T}, dims::Dims) = rand!(r, Array(T, dims))
rand(r::Range, dims::Int...) = rand(r, dims)
rand{T}(r::AbstractArray{T}, dims::Dims) = _rand!(r, Array(T, dims))
rand(r::AbstractArray, dims::Int...) = rand(r, dims)

## random BitArrays (AbstractRNG)

Expand Down
8 changes: 6 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4028,9 +4028,13 @@ A ``MersenneTwister`` RNG can generate random numbers of the following types: ``

Populate the array A with random values.

.. function:: rand(r, [dims...])
.. function:: rand(coll, [dims...])

Pick a random element or array of random elements from range ``r`` (for example, ``1:n`` or ``0:2:10``).
Pick a random element or array of random elements from the indexable collection ``coll`` (for example, ``1:n`` or ``['x','y','z']``).

.. function:: rand!(r, A)

Populate the array A with random values drawn uniformly from the range ``r``.

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

Expand Down
6 changes: 6 additions & 0 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ rand!(MersenneTwister(0), A)
@test A == [858542123778948672 5715075217119798169;
8690327730555225005 8435109092665372532]

# rand from AbstractArray
@test rand(0:3:1000) in 0:3:1000
coll = Any[2, UInt128(128), big(619), "string", 'c']
@test rand(coll) in coll
@test issubset(rand(coll, 2, 3), coll)

# randn
@test randn(MersenneTwister(42)) == -0.5560268761463861
A = zeros(2, 2)
Expand Down

0 comments on commit 66aa1a9

Please sign in to comment.