Skip to content

Random stdlib: randstring(rand(UInt8)) errors #54397

Open
@thomvet

Description

@thomvet

My aim here was to generate a random string of random length with a maximum length of 255 letters.
So, I tried the following:

using Random
s = randstring(rand(UInt8))

which errors with:

ERROR: MethodError: no method matching Random.UnsafeView(::Ptr{UInt8}, ::UInt8)

Closest candidates are:
  Random.UnsafeView(::Ptr{T}, ::Int64) where T
   @ Random C:\Users\thvt\AppData\Local\Programs\Julia-1.10.2\share\julia\stdlib\v1.10\Random\src\RNGs.jl:471

Stacktrace:
 [1] randstring(r::TaskLocalRNG, chars::Vector{UInt8}, n::UInt8)
   @ Random C:\Users\thvt\AppData\Local\Programs\Julia-1.10.2\share\julia\stdlib\v1.10\Random\src\misc.jl:79
 [2] randstring(n::UInt8)
   @ Random C:\Users\thvt\AppData\Local\Programs\Julia-1.10.2\share\julia\stdlib\v1.10\Random\src\misc.jl:90
 [3] top-level scope
   @ REPL[23]:1

If explicitly type-casting the result of rand() to Int64 (but not any other Integer type!), it works as expected:

using Random
s = randstring(Int64(rand(UInt8)))

It's not super transparent why that would be necessary and I would have expected it to "just work" with an UInt8 (or any other type <: Integer for that matter) as well.

Maybe one of the methods involved here is overly specific?

Output of versioninfo():

Julia Version 1.10.2
Commit bd47eca2c8 (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 24 × 12th Gen Intel(R) Core(TM) i7-12850HX
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, alderlake)
Threads: 24 default, 0 interactive, 12 GC (on 24 virtual cores)

Metadata

Metadata

Assignees

No one assigned

    Labels

    randomnessRandom number generation and the Random stdlib

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions