diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/math/expression.cpp | 17 | ||||
-rw-r--r-- | core/math/expression.h | 5 | ||||
-rw-r--r-- | core/math/math_funcs.cpp | 4 | ||||
-rw-r--r-- | core/math/math_funcs.h | 2 | ||||
-rw-r--r-- | core/math/random_number_generator.h | 21 | ||||
-rw-r--r-- | core/math/random_pcg.cpp | 15 | ||||
-rw-r--r-- | core/math/random_pcg.h | 2 |
7 files changed, 40 insertions, 26 deletions
diff --git a/core/math/expression.cpp b/core/math/expression.cpp index 1040f9e0e4..de7d2d2e2c 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -76,7 +76,8 @@ const char *Expression::func_name[Expression::FUNC_MAX] = { "randomize", "randi", "randf", - "rand_range", + "randf_range", + "randi_range", "seed", "rand_seed", "deg2rad", @@ -127,7 +128,7 @@ String Expression::get_func_name(BuiltinFunc p_func) { int Expression::get_func_argument_count(BuiltinFunc p_func) { switch (p_func) { case MATH_RANDOMIZE: - case MATH_RAND: + case MATH_RANDI: case MATH_RANDF: return 0; case MATH_SIN: @@ -178,7 +179,8 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) { case MATH_POW: case MATH_EASE: case MATH_STEPIFY: - case MATH_RANDOM: + case MATH_RANDF_RANGE: + case MATH_RANDI_RANGE: case MATH_POLAR2CARTESIAN: case MATH_CARTESIAN2POLAR: case LOGIC_MAX: @@ -397,17 +399,22 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant Math::randomize(); } break; - case MATH_RAND: { + case MATH_RANDI: { *r_return = Math::rand(); } break; case MATH_RANDF: { *r_return = Math::randf(); } break; - case MATH_RANDOM: { + case MATH_RANDF_RANGE: { VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); *r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]); } break; + case MATH_RANDI_RANGE: { + VALIDATE_ARG_NUM(0); + VALIDATE_ARG_NUM(1); + *r_return = Math::random((int)*p_inputs[0], (int)*p_inputs[1]); + } break; case MATH_SEED: { VALIDATE_ARG_NUM(0); uint64_t seed = *p_inputs[0]; diff --git a/core/math/expression.h b/core/math/expression.h index f2cfe6b1a6..80aeb98589 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -73,9 +73,10 @@ public: MATH_MOVE_TOWARD, MATH_DECTIME, MATH_RANDOMIZE, - MATH_RAND, + MATH_RANDI, MATH_RANDF, - MATH_RANDOM, + MATH_RANDF_RANGE, + MATH_RANDI_RANGE, MATH_SEED, MATH_RANDSEED, MATH_DEG2RAD, diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 4154713a87..80413e5c22 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -181,3 +181,7 @@ double Math::random(double from, double to) { float Math::random(float from, float to) { return default_rand.random(from, to); } + +int Math::random(int from, int to) { + return default_rand.random(from, to); +} diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index f83ee44f4a..827637bf2b 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -289,7 +289,7 @@ public: static double random(double from, double to); static float random(float from, float to); - static real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); } + static int random(int from, int to); static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) { // Check for exact equality first, required to handle "infinity" values. diff --git a/core/math/random_number_generator.h b/core/math/random_number_generator.h index 2e7941b345..08e41d6c1c 100644 --- a/core/math/random_number_generator.h +++ b/core/math/random_number_generator.h @@ -43,7 +43,7 @@ protected: static void _bind_methods(); public: - _FORCE_INLINE_ void set_seed(uint64_t seed) { randbase.seed(seed); } + _FORCE_INLINE_ void set_seed(uint64_t p_seed) { randbase.seed(p_seed); } _FORCE_INLINE_ uint64_t get_seed() { return randbase.get_seed(); } @@ -53,24 +53,11 @@ public: _FORCE_INLINE_ real_t randf() { return randbase.randf(); } - _FORCE_INLINE_ real_t randf_range(real_t from, real_t to) { return randbase.random(from, to); } + _FORCE_INLINE_ real_t randf_range(real_t p_from, real_t p_to) { return randbase.random(p_from, p_to); } - _FORCE_INLINE_ real_t randfn(real_t mean = 0.0, real_t deviation = 1.0) { return randbase.randfn(mean, deviation); } + _FORCE_INLINE_ real_t randfn(real_t p_mean = 0.0, real_t p_deviation = 1.0) { return randbase.randfn(p_mean, p_deviation); } - _FORCE_INLINE_ int randi_range(int from, int to) { - int range; - int min; - if (to > from) { - range = to - from + 1; - min = from; - } else if (to < from) { - range = from - to + 1; - min = to; - } else { // from == to - return from; - } - return randbase.rand(range) + min; - } + _FORCE_INLINE_ int randi_range(int p_from, int p_to) { return randbase.random(p_from, p_to); } RandomNumberGenerator() {} }; diff --git a/core/math/random_pcg.cpp b/core/math/random_pcg.cpp index 02257c38d9..e0768b9403 100644 --- a/core/math/random_pcg.cpp +++ b/core/math/random_pcg.cpp @@ -49,3 +49,18 @@ double RandomPCG::random(double p_from, double p_to) { float RandomPCG::random(float p_from, float p_to) { return randf() * (p_to - p_from) + p_from; } + +int RandomPCG::random(int p_from, int p_to) { + int range; + int min; + if (p_to > p_from) { + range = p_to - p_from + 1; + min = p_from; + } else if (p_to < p_from) { + range = p_from - p_to + 1; + min = p_to; + } else { // from == to + return p_from; + } + return rand(range) + min; +} diff --git a/core/math/random_pcg.h b/core/math/random_pcg.h index dfdae53eed..fe6b1b5639 100644 --- a/core/math/random_pcg.h +++ b/core/math/random_pcg.h @@ -133,7 +133,7 @@ public: double random(double p_from, double p_to); float random(float p_from, float p_to); - real_t random(int p_from, int p_to) { return (real_t)random((real_t)p_from, (real_t)p_to); } + int random(int p_from, int p_to); }; #endif // RANDOM_PCG_H |