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:

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.

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.

No, it should be much faster than the alternative:

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