summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorYuri Roubinsky <chaosus89@gmail.com>2019-04-07 08:32:04 +0300
committerGitHub <noreply@github.com>2019-04-07 08:32:04 +0300
commit134be5c728a8e54fe248c654daa30986d2153c1b (patch)
tree05d8027b550bd462b9377568fe7faf15e12f70e3 /core
parentde825df1210089b4f8427618bb758101d5ad379b (diff)
parent5f137925dccd8a7facc34d05d2652af6d03285ba (diff)
Merge pull request #27043 from Chaosus/randfn
Added gaussian distribution function to RNG
Diffstat (limited to 'core')
-rw-r--r--core/math/random_number_generator.cpp1
-rw-r--r--core/math/random_number_generator.h2
-rw-r--r--core/math/random_pcg.h9
3 files changed, 12 insertions, 0 deletions
diff --git a/core/math/random_number_generator.cpp b/core/math/random_number_generator.cpp
index fccc0f72fe..6add00c1d8 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("randfn", "mean", "deviation"), &RandomNumberGenerator::randfn, DEFVAL(0.0), DEFVAL(1.0));
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 66c77b8ccf..6b6bcdd2cd 100644
--- a/core/math/random_number_generator.h
+++ b/core/math/random_number_generator.h
@@ -55,6 +55,8 @@ public:
_FORCE_INLINE_ real_t randf_range(real_t from, real_t to) { return randbase.random(from, to); }
+ _FORCE_INLINE_ real_t randfn(real_t mean = 0.0, real_t deviation = 1.0) { return randbase.randfn(mean, deviation); }
+
_FORCE_INLINE_ int randi_range(int from, int to) {
unsigned int ret = randbase.rand();
return ret % (to - from + 1) + from;
diff --git a/core/math/random_pcg.h b/core/math/random_pcg.h
index cd721ef4d1..0d1b311c0d 100644
--- a/core/math/random_pcg.h
+++ b/core/math/random_pcg.h
@@ -31,6 +31,8 @@
#ifndef RANDOM_PCG_H
#define RANDOM_PCG_H
+#include <math.h>
+
#include "core/math/math_defs.h"
#include "thirdparty/misc/pcg.h"
@@ -61,6 +63,13 @@ public:
_FORCE_INLINE_ double randd() { return (double)rand() / (double)RANDOM_MAX; }
_FORCE_INLINE_ float randf() { return (float)rand() / (float)RANDOM_MAX; }
+ _FORCE_INLINE_ double randfn(double p_mean, double p_deviation) {
+ return p_mean + p_deviation * (cos(Math_TAU * randd()) * sqrt(-2.0 * log(randd()))); // Box-Muller transform
+ }
+ _FORCE_INLINE_ float randfn(float p_mean, float p_deviation) {
+ return p_mean + p_deviation * (cos(Math_TAU * randf()) * sqrt(-2.0 * log(randf()))); // Box-Muller transform
+ }
+
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); }