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

Support ranges as shape information in randn/rand #56940

Open
mtfishman opened this issue Jan 3, 2025 · 0 comments
Open

Support ranges as shape information in randn/rand #56940

mtfishman opened this issue Jan 3, 2025 · 0 comments
Labels
randomness Random number generation and the Random stdlib

Comments

@mtfishman
Copy link
Contributor

Array constructors like zeros, ones, and fill accept Base.OneTo to define the axes/dimensions/shape, but randn and rand don't:

julia> zeros(Float64, Base.OneTo.((2, 2)))
2×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.0

julia> ones(Float64, Base.OneTo.((2, 2)))
2×2 Matrix{Float64}:
 1.0  1.0
 1.0  1.0

julia> fill(2.0, Base.OneTo.((2, 2)))
2×2 Matrix{Float64}:
 2.0  2.0
 2.0  2.0

julia> randn(Float64, Base.OneTo.((2, 2)))
ERROR: MethodError: no method matching randn(::Type{Float64}, ::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}})
The function `randn` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  randn(::Type{T}) where T
   @ Random ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Random/src/normal.jl:225
  randn(::Type{T}, ::Integer...) where T
   @ Random ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Random/src/normal.jl:274
  randn(::Type{T}, ::NTuple{N, Int64} where N) where T
   @ Random ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Random/src/normal.jl:273
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> rand(Float64, Base.OneTo.((2, 2)))
ERROR: MethodError: no method matching rand(::Type{Float64}, ::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}})
The function `rand` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  rand(::Type{X}, ::Integer, Integer...) where X
   @ Random ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Random/src/Random.jl:294
  rand(::Type{X}, ::NTuple{N, Int64} where N) where X
   @ Random ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Random/src/Random.jl:291
  rand(::Type{X}) where X
   @ Random ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/Random/src/Random.jl:261
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

julia> versioninfo()
Julia Version 1.12.0-DEV.1825
Commit ed2cb490505 (2025-01-02 20:05 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: macOS (arm64-apple-darwin24.0.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

I think it would be nice for generic code to support that.

@inkydragon inkydragon added the randomness Random number generation and the Random stdlib label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
randomness Random number generation and the Random stdlib
Projects
None yet
Development

No branches or pull requests

2 participants