# Static Array With Random Numbers From a Uniform Distribution

**URL:** <https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513>\
**Category:** New to Julia\
**Tags:** statistics, staticarrays\
**Created:** [December 12, 2023, 4:24pm UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513 "2023-12-12T16:24:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hakanc](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hakanc](https://discourse.julialang.org/u/hakanc)\
**Post date:** [December 12, 2023, 4:24pm UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/1 "2023-12-12T16:24:05Z")

</div>

I am trying to define a static array with numbers pulled from a uniform distribution (`using Distributions`) from -1 to 1. It is easy to generate a matrix:

```julia
pvec=rand(Uniform(-1, 1), 3,10)

```

But I want to have a static array (`using StaticArrays`) for which I can write `rand(SVector{3,Float64},10)`, but I don’t know how to combine both. How can I define a static array and keep the uniform distribution information? I know I can do scaling and addition to get a distribution from -1 to 1 from the second command but it is not as fast as I would like.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [December 12, 2023, 5:05pm UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/2 "2023-12-12T17:05:59Z")

</div>

It’s clumsy, but here’s a solution that relies on reinterpreting the vector of `SVector` as a “typical” array so that it can be filled by `rand!`:

```julia-repl
julia> using Random, StaticArrays, Distributions

julia> z = Vector{SVector{3,Float32}}(undef, 10);

julia> rand!(Uniform(-1, 1), reinterpret(Float32, z))
30-element reinterpret(Float32, ::Vector{SVector{3, Float32}}):
 -0.03727758
  0.6890855
 -0.91755855
  0.25815475
  ⋮
  0.48348153
 -0.8701681
  0.63198125
  0.57068086

julia> z
10-element Vector{SVector{3, Float32}}:
 [-0.03727758, 0.6890855, -0.91755855]
 [0.25815475, 0.9596908, 0.95804846]
 [0.4309975, 0.38259137, 0.5585474]
 [0.16815412, 0.19676924, 0.5617609]
 [0.9677615, -0.66337335, -0.17191088]
 [-0.25808072, -0.2981007, -0.10571146]
 [-0.87585616, -0.08150554, -0.4999045]
 [-0.36705136, 0.9787103, 0.12711072]
 [-0.85927045, 0.7996801, 0.48348153]
 [-0.8701681, 0.63198125, 0.57068086]

```

But there’s probably a more idiomatic way. Hopefully someone else can point you towards it. I haven’t really used `Distributions` so I’m not familiar with the interfaces it provides.

By the way, it’s good practice to mention the packages you’re using. If someone didn’t recognize `Uniform` and `SVector` then they wouldn’t know you were using `Distributions` and `StaticArrays` and it would be difficult to help you.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 12, 2023, 5:07pm UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/3 "2023-12-12T17:07:55Z")

</div>

You can do

```julia
[2 .* rand(SVector{3, Float64}) .- 1 for _ in 1:10]

```

but it doesn’t generalize very well to other distributions.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 12, 2023, 5:42pm UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/4 "2023-12-12T17:42:09Z")

</div>

Or `reinterpret` the other way:

```julia
julia> reinterpret(SVector{3, Float64},rand(Uniform(-1,1),prod((3,10))))
10-element reinterpret(SVector{3, Float64}, ::Vector{Float64}):
 [0.342863141712473, 0.3758805838896866, 0.27241838039458677]
...

```

---

<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:** [December 12, 2023, 6:40pm UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/5 "2023-12-12T18:40:01Z")

</div>

See [Static array with uniform distribution](https://discourse.julialang.org/t/static-array-with-uniform-distribution/106961)

---

<div class="post-metadata">

**Author:** ![hakanc](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hakanc](https://discourse.julialang.org/u/hakanc)\
**Post date:** [December 13, 2023, 9:41am UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/6 "2023-12-13T09:41:12Z")

</div>

Yes but this seems to be adding significant computation cost in my case.

---

<div class="post-metadata">

**Author:** ![hakanc](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hakanc](https://discourse.julialang.org/u/hakanc)\
**Post date:** [December 13, 2023, 9:48am UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/7 "2023-12-13T09:48:54Z")

</div>

I will keep the suggestion in mind for future questions, thank you!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 13, 2023, 10:25am UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/8 "2023-12-13T10:25:02Z")

</div>

> [@hakanc](#):
>
> Yes but this seems to be adding significant computation cost in my case.

No, it should be much faster than the alternative:

```julia
foo(N) = [2 .* rand(SVector{3, Float64}) .- 1 for _ in 1:N]

function bar(N)
    z = Vector{SVector{3,Float64}}(undef, N)
    rand!(Uniform(-1, 1), reinterpret(Float64, z))
    return z
end

```

Timing:

```julia
julia> using BenchmarkTools

julia> @btime foo(10);
  96.635 ns (1 allocation: 304 bytes)

julia> @btime bar(10);
  414.141 ns (1 allocation: 304 bytes)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 13, 2023, 10:37am UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/9 "2023-12-13T10:37:17Z")

</div>

I think OP meant compared to the simple `rand(SVector..)` construct, although even then I don’t see a material slowdown:

```julia
julia> baz(N) = rand(SVector{3,Float64}, N)
baz (generic function with 1 method)

julia> @btime foo(10);
  70.961 ns (1 allocation: 304 bytes)

julia> @btime bar(10);
  297.378 ns (1 allocation: 304 bytes)

julia> @btime baz(10);
  64.082 ns (1 allocation: 304 bytes)

```

---

<div class="post-metadata">

**Author:** ![hakanc](https://avatars.discourse-cdn.com/v4/letter/h/9de0a6/32.png) [@hakanc](https://discourse.julialang.org/u/hakanc)\
**Post date:** [December 13, 2023, 10:56am UTC](https://discourse.julialang.org/t/static-array-with-random-numbers-from-a-uniform-distribution/107513/10 "2023-12-13T10:56:44Z")

</div>

I am new to Julia so I may be doing something wrong. I will try again and see
