Skip to content

Commit

Permalink
make rand work with AbstractArray instead of only with Range
Browse files Browse the repository at this point in the history
This implements the generalization suggested by @ivarne
(JuliaLang#8257 (comment))
or by @lindahua
(JuliaLang#6003 (comment)).
This change is very simple thanks to commit 48f27bc.
  • Loading branch information
rfourquet authored and rafael committed Sep 30, 2014
1 parent 39413ed commit 6eda92f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ rand{T<:Number}(::Type{T}) = error("no random number generator for type $T; try
rand{T<:Number}(::Type{T}, dims::Int...) = rand(T, dims)


## Generate random integer within a range
## Generate random integer within a range 1:n

# remainder function according to Knuth, where rem_knuth(a, 0) = a
rem_knuth(a::Uint, b::Uint) = a % (b + (b == 0)) + a * (b == 0)
Expand Down Expand Up @@ -208,18 +208,24 @@ function rand{T<:Integer, U<:Unsigned}(g::RandIntGen{T,U})
itrunc(T, one(U) + rem_knuth(x, g.k))
end

rand{T}(r::Range{T}) = r[rand(randintgen(length(r)))]

function rand!(r::Range, A::AbstractArray)
## 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) = r[rand(randintgen(length(r)))]

# Arrays of random integers

function rand!(r::AbstractArray, A::AbstractArray)
g = randintgen(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 Bools
Expand Down

0 comments on commit 6eda92f

Please sign in to comment.