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

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

Open
thomvet opened this issue May 8, 2024 · 0 comments · May be fixed by #54402
Open

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

thomvet opened this issue May 8, 2024 · 0 comments · May be fixed by #54402
Labels
domain:randomness Random number generation and the Random stdlib

Comments

@thomvet
Copy link

thomvet commented May 8, 2024

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)
@Seelengrab Seelengrab added the domain:randomness Random number generation and the Random stdlib label May 8, 2024
Seelengrab added a commit to Seelengrab/julia that referenced this issue May 8, 2024
Fixes JuliaLang#54397

What remains is that the effectively allowed input still needs
to be convertible to `Int`. So things like `typemax(Int128)` won't
work, and neither will negative values.
@Seelengrab Seelengrab linked a pull request May 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants