# Is there a way to select the type of indices Ti with sprand()?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-select-the-type-of-indices-ti-with-sprand/127259>\
**Category:** General Usage\
**Tags:** sparsearrays\
**Created:** [March 22, 2025, 11:34am UTC](https://discourse.julialang.org/t/is-there-a-way-to-select-the-type-of-indices-ti-with-sprand/127259 "2025-03-22T11:34:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AntoineBut](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoinebut/32/213352_2.png) [@AntoineBut](https://discourse.julialang.org/u/AntoineBut)\
**Post date:** [March 22, 2025, 11:34am UTC](https://discourse.julialang.org/t/is-there-a-way-to-select-the-type-of-indices-ti-with-sprand/127259/1 "2025-03-22T11:34:19Z")

</div>

The function `sprand()` has methods to specify the type of the generated elements `{Tv}`. But a SparseArray has two type parameters, the second being the type used to stores the indices `{Ti}`. Does anyone have a magic trick to generate a random sparse matrix using Int32 (or others) instead of Int64?

The best way I found is to generate the matrix using `A = sprand(Tv, m, n, fill)` and then type cast the internal arrays `A.rowptr` and `A.colval`, but it doesn’t feel right.

Would a new method of sprand taking two types in parameter make sense? Is there a reason it doesn’t exist yet?

---

<div class="post-metadata">

**Author:** ![AntoineBut](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoinebut/32/213352_2.png) [@AntoineBut](https://discourse.julialang.org/u/AntoineBut)\
**Post date:** [March 22, 2025, 12:15pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-select-the-type-of-indices-ti-with-sprand/127259/2 "2025-03-22T12:15:21Z")

</div>

I could probably use the `similar()` function as posted 3 hours ago [here](https://discourse.julialang.org/t/why-sprand-does-not-accept-a-distribution-arg/127255/4). Thanks @gdalle.

I am still wondering wether a `sprand()` method taking two types in parameters would be useful though 🤔
