Skip to content

Commit

Permalink
Merge pull request JuliaLang#11178 from ScottPJones/spj/randchar
Browse files Browse the repository at this point in the history
Simplify and improve performance of rand(Char)
  • Loading branch information
jakebolewski committed May 7, 2015
2 parents 0aa5cd3 + f410620 commit 2bd0de3
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,10 @@ rand(r::MersenneTwister, ::Type{Int128}) = reinterpret(Int128, rand(r, UInt128)
rand{T<:Real}(r::AbstractRNG, ::Type{Complex{T}}) = complex(rand(r, T), rand(r, T))

# random Char values
# use simple rejection sampling over valid Char codepoint range
# returns a random valid Unicode scalar value (i.e. 0 - 0xd7ff, 0xe000 - # 0x10ffff)
function rand(r::AbstractRNG, ::Type{Char})
while true
c = rand(0x00000000:0x0010fffd)
if is_valid_char(c)
return reinterpret(Char,c)
end
end
c = rand(0x00000000:0x0010f7ff)
(c < 0xd800) ? Char(c) : Char(c+0x800)
end

## Arrays of random numbers
Expand Down

0 comments on commit 2bd0de3

Please sign in to comment.