Browse Source

Implement Fisher-Yates shuffle

Feoramund 1 year ago
parent
commit
3e449e93dd
1 changed files with 2 additions and 2 deletions
  1. 2 2
      core/math/rand/rand.odin

+ 2 - 2
core/math/rand/rand.odin

@@ -789,8 +789,8 @@ shuffle :: proc(array: $T/[]$E, r: ^Rand = nil) {
 		return
 		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]
 		array[i], array[j] = array[j], array[i]
 	}
 	}
 }
 }