Implement Fisher-Yates shuffle

This commit is contained in:
Feoramund
2024-04-15 17:07:05 -04:00
parent 436c5dc40c
commit 3e449e93dd

View File

@@ -789,8 +789,8 @@ shuffle :: proc(array: $T/[]$E, r: ^Rand = nil) {
return
}
for i := i64(0); i < n; i += 1 {
j := int63_max(n, r)
for i := i64(n - 1); i > 0; i -= 1 {
j := int63_max(i + 1, r)
array[i], array[j] = array[j], array[i]
}
}