Browse Source

Improve rand.shuffle further by splitting into 64-bit and 32-bit parts

gingerBill 1 year ago
parent
commit
0d881e1561
1 changed files with 7 additions and 1 deletions
  1. 7 1
      core/math/rand/rand.odin

+ 7 - 1
core/math/rand/rand.odin

@@ -618,10 +618,16 @@ shuffle :: proc(array: $T/[]$E, gen := context.random_generator) {
 		return
 	}
 
-	for i := i64(n - 2); i >= 0; i -= 1 {
+	i := n - 1
+	for ; i > (1<<31 - 2); i -= 1 {
 		j := int63_max(i + 1, gen)
 		array[i], array[j] = array[j], array[i]
 	}
+
+	for ; i > 0; i -= 1 {
+		j := int31_max(i32(i + 1), gen)
+		array[i], array[j] = array[j], array[i]
+	}
 }
 
 /*