Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
This version of `rand(r::Range)` and `rand!(r::Range,a::AbstractArray)`
uses getindex instead of trying to calculate the exact value of the
range. This is good because we avoid duplicating the getindex logic in
`FloatRange`
  • Loading branch information
ivarne committed Sep 8, 2014
1 parent 916f965 commit 128d10f
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ 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}) = convert(T, first(r) + rand(0:(length(r)-1)) * step(r))
rand{T}(r::Range{T}) = r[rand(1:(length(r)))]

function rand!(g::RandIntGen, A::AbstractArray)
for i = 1 : length(A)
Expand All @@ -219,17 +219,9 @@ end
rand!{T<:Union(Signed,Unsigned,Bool,Char)}(r::UnitRange{T}, A::AbstractArray) = rand!(RandIntGen(r), A)

function rand!{T}(r::Range{T}, A::AbstractArray)
g = RandIntGen(0:(length(r)-1))
f = first(r)
s = step(r)
if s == 1
for i = 1 : length(A)
@inbounds A[i] = convert(T, f + rand(g))
end
else
for i = 1 : length(A)
@inbounds A[i] = convert(T, f + rand(g) * s)
end
g = RandIntGen(1:(length(r)))
for i = 1 : length(A)
@inbounds A[i] = r[rand(g)]
end
return A
end
Expand Down

0 comments on commit 128d10f

Please sign in to comment.