浏览代码

Add `core:math/rand.choice`

Jeroen van Rijn 3 年之前
父节点
当前提交
a51943e27f
共有 1 个文件被更改,包括 9 次插入0 次删除
  1. 9 0
      core/math/rand/rand.odin

+ 9 - 0
core/math/rand/rand.odin

@@ -182,3 +182,12 @@ shuffle :: proc(array: $T/[]$E, r: ^Rand = nil) {
 		array[i], array[j] = array[j], array[i]
 	}
 }
+
+// Returns a random element from the given slice
+choice :: proc(array: $T/[]$E, r: ^Rand = nil) -> (res: E) {
+	n := i64(len(array))
+	if n < 1 {
+		return E{}
+	}
+	return array[int63_max(n, r)]
+}