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

add rand(::String) #22224

Merged
merged 7 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test for empty strings (StefanKarpinski) and fix bad usage a GLOBAL_RNG
  • Loading branch information
rfourquet committed Jun 13, 2017
commit 1deb0c5059da470e7b3975aa9d795f623494cf8c
4 changes: 2 additions & 2 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ rand(s::AbstractString) = rand(GLOBAL_RNG, s)
isvalid_unsafe(s::String, i) = !Base.is_valid_continuation(unsafe_load(pointer(s), i))

function rand(rng::AbstractRNG, s::String)
rg = Base.Random.RangeGenerator(1:s.len)
rg = RangeGenerator(1:s.len)
while true
pos = rand(Base.Random.GLOBAL_RNG, rg)
pos = rand(rng, rg)
isvalid_unsafe(s, pos) && return s[pos]
end
end
Expand Down
3 changes: 2 additions & 1 deletion test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ for rng in ([], [MersenneTwister(0)], [RandomDevice()])
end
end
for C in [1:0, Dict(), Set(), IntSet(), Int[],
GenericDict(Dict()), GenericSet(Set())]
GenericDict(Dict()), GenericSet(Set()),
"", Base.Test.GenericString("")]
@test_throws ArgumentError rand(rng..., C)
@test_throws ArgumentError rand(rng..., C, 5)
end
Expand Down