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

Fix #8257 #8273

Merged
merged 1 commit into from
Sep 10, 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
18 changes: 5 additions & 13 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 @@ -218,18 +218,10 @@ 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
function rand!(r::Range, A::AbstractArray)
g = RandIntGen(1:(length(r)))
for i = 1 : length(A)
@inbounds A[i] = r[rand(g)]
end
return A
end
Expand Down
2 changes: 1 addition & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ function map!(f::Callable, dest, r::Range)
end

function in(x, r::Range)
n = step(r) == zero(step(r)) ? 1 : iround((x-first(r))/step(r))+1
n = step(r) == 0 ? 1 : iround((x-first(r))/step(r))+1
n >= 1 && n <= length(r) && r[n] == x
end

Expand Down
6 changes: 6 additions & 0 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,9 @@ a = uuid4()
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-446655440000")
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-44665544000098")
@test_throws ArgumentError UUID("z50e8400-e29b-41d4-a716-446655440000")

#issue 8257
i8257 = 1:1/3:100
for i = 1:100
@test rand(i8257) in i8257
end