From 75d69fb4ecfe3bc925bd27700782d884e88369cc Mon Sep 17 00:00:00 2001 From: poke1024 Date: Wed, 10 Jan 2018 19:36:53 +0100 Subject: Add shuffle() method to Array --- core/array.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'core/array.cpp') diff --git a/core/array.cpp b/core/array.cpp index c53fea1f28..0ddac1662c 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -266,6 +266,20 @@ Array &Array::sort_custom(Object *p_obj, const StringName &p_function) { return *this; } +void Array::shuffle() { + + const int n = _p->array.size(); + if (n < 2) + return; + Variant *data = _p->array.ptrw(); + for (int i = n - 1; i >= 1; i--) { + const int j = Math::rand() % (i + 1); + const Variant tmp = data[j]; + data[j] = data[i]; + data[i] = tmp; + } +} + template _FORCE_INLINE_ int bisect(const Vector &p_array, const Variant &p_value, bool p_before, const Less &p_less) { -- cgit v1.2.3