6 ms·
Just to avoid potential confusion, the claim is that this is a function that generates a random permutation: pub fn shuffle(g: *Gen, T: type, slice: []T) v
by matklad 10mo ago
Just to avoid potential confusion, the claim is that this is a function that generates a random
permutation:
pub fn shuffle(g: *Gen, T: type, slice: []T) void {
if (slice.len <= 1) return;
for (0..slice.len - 1) |i| {
const j = g.range_inclusive(u64, i, slice.len - 1);
std.mem.swap(T, &slice[i], &slice[j]);
}
}
And this is a function that enumerates all permutations, in order, exactly once:
pub fn shuffle(g: *Gen, T: type, slice: []T) void {
if (slice.len <= 1) return;
for (0..slice.len - 1) |i| {
const j = g.range_inclusive(u64, i, slice.len - 1);
std.mem.swap(T, &slice[i], &slice[j]);
}
}
Yes, they are exactly the same function. What matters is Gen. If it looks like this
https://github.com/tigerbeetle/tigerbeetle/blob/809fe06a2ffcc09a92110f6aa46546ba9a8ad2bf/src/stdx/prng.zig#L134-L149 https://github.com/tigerbeetle/tigerbeetle/blob/809fe06a2ffc...
then you get a random permutation. If it rather looks like this
https://github.com/tigerbeetle/tigerbeetle/blob/809fe06a2ffcc09a92110f6aa46546ba9a8ad2bf/src/testing/exhaustigen.zig#L77-L86 https://github.com/tigerbeetle/tigerbeetle/blob/809fe06a2ffc...
you enumerate all permutations.