Creating and modifying an array of random floats

Instead of the while loops you can use for loops with pointer capture to modify the current element:

// Populate `arr` with random numbers.
for (&arr) |*dst| dst.* = rand.float(f64);

// Iterate over `arr`, modifying each elem as I go.
for (&arr) |*dst| dst.* *= 10;

And if you needed the index you could use:

for (&arr, 0..) |*dst, i| dst.* += @as(f64, @floatFromInt(i + 1));

But while loops are really good for complex loops, beyond just simply iterating over everything.

4 Likes