In short: Why can I use
let array: [[f64; width]; length] = core::array::from_fn(|_| rng.gen());
but not
let array: [[f64; width]; length] = core::array::from_fn(|_| rng.gen_range(0.0..1.0));
It's not terribly that it doesn't work, but I don't understand the reason here.
Making an array with from_fn
only does one level: from_fn(() -> T)
creates a [T; _]
. So rng.gen()
is building the full inner array type : [f64; width]
. This works because it can generate anything that implements a standard distribution which is implemented for arrays where the element type implements a standard distribution.
So when you do the same with rng.gen_range(0.0..1.0)
, you are asking for it to make [f64; width]
from a value between zero and one which doesn't quite make sense. You may think it works transparently to generate multiple values between zero and one, but that's not how the function works: gen_range(T..T)
will generate a T
.
So you need something like this if you want a multi-dimensional array with random values between zero and one.
let array: [[f64; width]; length] = core::array::from_fn(|_|
core::array::from_fn(|_| rng.gen_range(0.0..1.0))
);