From 3e449e93dd60d80c89657d42efa85cb3cc4e8cc3 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:07:05 -0400 Subject: [PATCH] Implement Fisher-Yates shuffle --- core/math/rand/rand.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/math/rand/rand.odin b/core/math/rand/rand.odin index 560dc8379..d6a20bd1e 100644 --- a/core/math/rand/rand.odin +++ b/core/math/rand/rand.odin @@ -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] } }