@@ -1618,15 +1618,42 @@ end
16181618Base. show (io:: IO , u:: UUID ) = write (io, Base. repr (u))
16191619
16201620# return a random string (often useful for temporary filenames/dirnames)
1621+
1622+ """
1623+ randstring([rng=GLOBAL_RNG], [chars], [len=8])
1624+
1625+ Create a random string of length `len`, consisting of characters from
1626+ `chars`, which defaults to the set of upper- and lower-case letters
1627+ and the digits 0-9. The optional `rng` argument specifies a random
1628+ number generator, see [Random Numbers](@ref).
1629+
1630+ # Examples
1631+ ```jldoctest
1632+ julia> srand(0); randstring()
1633+ "c03rgKi1"
1634+
1635+ julia> randstring(MersenneTwister(0), 'a':'z', 6)
1636+ "wijzek"
1637+
1638+ julia> randstring("ACGT")
1639+ "TATCGGTC"
1640+ ```
1641+
1642+ !!! note
1643+ `chars` can be any collection of characters, of type `Char` or
1644+ `UInt8` (more efficient), provided [`rand`](@ref) can randomly
1645+ pick characters from it.
1646+ """
1647+ function randstring end
1648+
16211649let b = UInt8[' 0' :' 9' ;' A' :' Z' ;' a' :' z' ]
16221650 global randstring
1623- randstring (r:: AbstractRNG , n:: Int ) = String (b[ rand (r, 1 : length (b) , n)] )
1624- randstring (r:: AbstractRNG ) = randstring (r,8 )
1625- randstring (n:: Int ) = randstring (GLOBAL_RNG, n)
1626- randstring () = randstring (GLOBAL_RNG)
1651+ randstring (r:: AbstractRNG , chars = b, n:: Integer = 8 ) = String (rand (r, chars , n))
1652+ randstring (r:: AbstractRNG , n :: Integer ) = randstring (r, b, n )
1653+ randstring (chars = b, n:: Integer = 8 ) = randstring (GLOBAL_RNG, chars , n)
1654+ randstring (n :: Integer ) = randstring (GLOBAL_RNG, b, n )
16271655end
16281656
1629-
16301657# Fill S (resized as needed) with a random subsequence of A, where
16311658# each element of A is included in S with independent probability p.
16321659# (Note that this is different from the problem of finding a random
0 commit comments