In-place fills
In-place methods mutate the destination and leave the key unchanged.
Reuse an existing buffer
A keyed fill matches the allocating method for the same type and shape.
using ImmutableRNGs, Random, Statistics
key = Philox4x32(42)
buffer = Vector{Float64}(undef, 1024)
rand!(key, buffer)
buffer == rand(key, Float64, length(buffer))trueRefilling with the same key repeats the values. Derive a new key for each logical batch.
first_batch = copy(buffer)
rand!(key, buffer)
repeated = buffer == first_batch
randn!(subrng(key, 2), buffer)
fresh = buffer != first_batch
(repeated, fresh)(true, true)Address each batch
Stable batch addresses permit buffer reuse and direct replay.
function relu_mean(key, buffer, batches)
total = 0.0
for batch in 1:batches
randn!(subrng(key, batch), buffer)
total += sum(x -> max(x, 0.0), buffer)
end
return total / (batches * length(buffer))
end
estimate = relu_mean(key, buffer, 64)
randn!(subrng(key, 17), buffer)
batch_17 = mean(x -> max(x, 0.0), buffer)
(estimate, batch_17)(0.398656877178654, 0.41334245833728456)The key for batch 17 identifies its random data. Replaying that batch does not require batches 1 through 16.
Use cursor-backed rand!, randn!, or randexp! when batches must form one sequential chain. The wrapper reserves and commits the complete fill before it writes the destination.
See 13_inplace_fills.jl and 07_gpu_generate.jl.