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

enable rand(::AbstractArray, [dims]) (take 3) #9049

Merged
merged 1 commit into from
Nov 18, 2014
Merged
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
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