summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2018-12-12 14:07:47 +0100
committerGitHub <noreply@github.com>2018-12-12 14:07:47 +0100
commita1954e51e8413d3dbfcafc03fd400c16cae7fce9 (patch)
treed73a2e8b3b18a5682454eb37be6cc6d78f315bfe /core/math
parentab1a80504e81430be1221a6653fc3934006bbe50 (diff)
parentd376be2bf4c8c8b5bfcb650647ebb73605f8fc81 (diff)
Merge pull request #24135 from Chaosus/rng
Added ranged integer generation function to RNG class
Diffstat (limited to 'core/math')
-rw-r--r--core/math/random_number_generator.cpp3
-rw-r--r--core/math/random_number_generator.h7
2 files changed, 8 insertions, 2 deletions
diff --git a/core/math/random_number_generator.cpp b/core/math/random_number_generator.cpp
index e4ec0dac99..9f0a5bc992 100644
--- a/core/math/random_number_generator.cpp
+++ b/core/math/random_number_generator.cpp
@@ -40,6 +40,7 @@ void RandomNumberGenerator::_bind_methods() {
ClassDB::bind_method(D_METHOD("randi"), &RandomNumberGenerator::randi);
ClassDB::bind_method(D_METHOD("randf"), &RandomNumberGenerator::randf);
- ClassDB::bind_method(D_METHOD("rand_range", "from", "to"), &RandomNumberGenerator::rand_range);
+ ClassDB::bind_method(D_METHOD("randf_range", "from", "to"), &RandomNumberGenerator::randf_range);
+ ClassDB::bind_method(D_METHOD("randi_range", "from", "to"), &RandomNumberGenerator::randi_range);
ClassDB::bind_method(D_METHOD("randomize"), &RandomNumberGenerator::randomize);
}
diff --git a/core/math/random_number_generator.h b/core/math/random_number_generator.h
index 557863fdbd..078bfbed60 100644
--- a/core/math/random_number_generator.h
+++ b/core/math/random_number_generator.h
@@ -53,7 +53,12 @@ public:
_FORCE_INLINE_ real_t randf() { return randbase.randf(); }
- _FORCE_INLINE_ real_t rand_range(real_t from, real_t to) { return randbase.random(from, to); }
+ _FORCE_INLINE_ real_t randf_range(real_t from, real_t to) { return randbase.random(from, to); }
+
+ _FORCE_INLINE_ int randi_range(int from, int to) {
+ unsigned int ret = randbase.rand();
+ return ret % (to - from + 1) + from;
+ }
RandomNumberGenerator();
};