@@ -264,10 +264,6 @@ Pick a random element or array of random elements from the set of values specifi
264
264
265
265
`S` defaults to [`Float64`](@ref).
266
266
267
- Note that the complexity of `rand(rng, s::AbstractString)` is linear
268
- in the length of `s` if `s` is not of type `String`. If called more
269
- than a few times, you should use `rand(rng, collect(s))` instead.
270
-
271
267
# Examples
272
268
273
269
```julia-repl
@@ -691,32 +687,27 @@ rand(rng::AbstractRNG, r::AbstractArray, dims::Int...) = rand(rng, r, dims)
691
687
692
688
# rand from a string
693
689
694
- rand (rng:: AbstractRNG , s:: AbstractString ) = rand_iter (rng, s, Base. iteratorsize (s))
695
- rand (s:: AbstractString ) = rand (GLOBAL_RNG, s)
696
-
697
- # # optimization for String
698
-
699
690
isvalid_unsafe (s:: String , i) = ! Base. is_valid_continuation (unsafe_load (pointer (s), i))
700
691
701
- function rand (rng:: AbstractRNG , s:: String )
702
- rg = RangeGenerator (1 : s. len)
692
+ function rand (rng:: AbstractRNG , s:: String ):: Char
693
+ g = RangeGenerator (1 : s. len)
703
694
while true
704
- pos = rand (rng, rg )
695
+ pos = rand (rng, g )
705
696
isvalid_unsafe (s, pos) && return s[pos]
706
697
end
707
698
end
708
699
709
- # # rand from an iterator
710
-
711
- rand_iter (rng, s, :: Base.IteratorSize ) = throw (ArgumentError (" iterator must have a known length" ))
712
-
713
- function rand_iter (rng:: AbstractRNG , s, :: Union{Base.HasShape,Base.HasLength} ):: eltype (s)
714
- pos = rand (rng, 1 : length (s))
715
- for (i, c) in enumerate (s)
716
- i == pos && return c
700
+ function rand (rng:: AbstractRNG , s:: AbstractString ):: Char
701
+ g = RangeGenerator (1 : endof (s))
702
+ while true
703
+ try # the generic `isvalid` includes an equivalent try/catch statement
704
+ return s[rand (rng, g)]
705
+ end
717
706
end
718
707
end
719
708
709
+ rand (s:: AbstractString ) = rand (GLOBAL_RNG, s)
710
+
720
711
# # rand from a string for arrays
721
712
# we use collect(str), which is most of the time more efficient than specialized methods
722
713
# (except maybe for very small arrays)
0 commit comments