# Rand() doesn't work with dims as tuple of Uint

**URL:** https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716
**Category:** New to Julia
**Tags:** question, random
**Created:** [April 9, 2024, 10:09am UTC](https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716 "2024-04-09T10:09:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![alex180500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex180500/32/44358_2.png) [@alex180500](https://discourse.julialang.org/u/alex180500)
#### Post date: [April 9, 2024, 10:09am UTC](https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716/1 "2024-04-09T10:09:19Z")

</div>

I’m trying to create random numbers in a matrix given by another variable that is a UInt32. For example I have something like this:

```julia
test_uint::UInt32 = 4
rand(Float64, (test_uint, 10))

```

And get the following error

```julia
ERROR: MethodError: no method matching rand(::Type{Float64}, ::Tuple{UInt32, Int64})

```

Why is the case? What are the supported types for dims? is it only Int64?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 9, 2024, 10:32am UTC](https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716/2 "2024-04-09T10:32:30Z")

</div>

I’m not sure if the API always strictly adheres to it, but there is a type alias `Dims` for `Tuple{Vararg{Int, N}} where N` used for `AbstractArray` dimensions. Some functions like `zeros` accept `dims` arguments with more integer types, but even then it gets wrangled into `Dims` for the output:

```julia
julia> typeof(size(zeros(Float64, (UInt32(4), 10)))) # 64-bit system
Tuple{Int64, Int64}

```

On v1.10.2, all the Base methods for `rand` appear to annotate `dims` with `::Integer...` or `::Dims`. So, `rand(Float64, UInt(4), 10)` will work, but `rand(Float64, (UInt(4), 10))` won’t, you’ll need `rand(Float64, (4, 10))`.

---

<div class="post-metadata">

### Author: ![alex180500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex180500/32/44358_2.png) [@alex180500](https://discourse.julialang.org/u/alex180500)
#### Post date: [April 9, 2024, 1:40pm UTC](https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716/3 "2024-04-09T13:40:16Z")

</div>

Thanks a lot for the answer! In my opinion this is super confusing and should just be leveled out, also, negative dims makes no real sense so using a UInt should be the preferred choice, no? Maybe there are other reasons for this?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 9, 2024, 2:04pm UTC](https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716/4 "2024-04-09T14:04:23Z")

</div>

> [@alex180500](#):
>
> What are the supported types for dims? is it only Int64?

In general, it’s only `Int` (`== Int64` on a 64-bit machine), though a few functions support more general `Integer` arguments and it’s been slowly expanded over time … in particular, _tuples_ of dimensions still have to be `Int`. This was discussed way back in 2014: [accept more Integer types for dims::Int... arguments by stevengj · Pull Request #7956 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/7956) (see also issues that link this).

> [@alex180500](#):
>
> negative dims makes no real sense so using a UInt should be the preferred choice, no?

See [this comment from 2014](https://groups.google.com/g/julia-users/c/RX8sFQHvEV4/m/ttxfYufL7WUJ) by @StefanKarpinski and this discourse discussion: [Why `Int` instead of `UInt` for indexing?](https://discourse.julialang.org/t/why-int-instead-of-uint-for-indexing/63589)

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 10, 2024, 1:52am UTC](https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716/5 "2024-04-10T01:52:40Z")

</div>

> [@alex180500](#):
>
> negative dims makes no real sense so using a UInt should be the preferred choice, no?

This topic also extends far beyond Julia. Many languages opt for signed integer indices, and even the languages that use unsigned integer indices have a lot of nuance and caveats when you involve arithmetic. I’m linking [the inventor of C++ Bjarne Stroustrup and the ISO C++ standards committee convener Herb Sutter](https://www.youtube.com/watch?v=Puio5dly9N8&t=2559s) suggesting signed integer types, warning of mixing integer types, and griping about such a mix stuck in the standard library. As for your question in particular:

> Stroustrup: …Now, when people use unsigned numbers, they usually have a reason. And the reason will be something like, “well it can’t be negative”, or “I need an extra bit”. If you need an extra bit, I am very reluctant to believe you that you really need it, and I don’t think that’s a good reason. When you think you can’t have negative numbers, you will have somebody who initialize your unsigned with −2, and think they get −2, and things like that. It is just highly error-prone…  
> Sutter: …If you are writing a very large data structure, the only case where you would really care about the unsigned is if you know it’s an array of characters that’s going to be bigger than half of memory. I don’t know of any other case where it matters…

Array elements are often larger than 1 byte, which needs many times fewer integer indices than memory addresses, even for such an array completely filling addressable memory. 64-bit systems were mainstreamed before RAM exceeded the 4GiB limit of 32-bit systems, and we’ll likely move away from 64-bit systems before we ever have 16EiB RAM. We can afford to “waste” half of an integer range on indexing because arrays are just so rarely ≤1 byte per element AND large enough.

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [April 10, 2024, 6:16am UTC](https://discourse.julialang.org/t/rand-doesnt-work-with-dims-as-tuple-of-uint/112716/6 "2024-04-10T06:16:32Z")

</div>

> [@alex180500](#):
>
> I’m trying to create random numbers in a matrix given by another variable that is a UInt32. For example I have something like this:
> 
> ```julia
> test_uint::UInt32 = 4
> rand(Float64, (test_uint, 10))
> 
> ```

Just for the record, note that you can splat the tuple in this case:

```julia
test_uint::UInt32 = 4
rand(Float64, (test_uint, 10)...)

```

or

```julia
rand(Float64, test_uint, 10)

```

works fine.
