Browse Source

Added generator for approximatly normal distributed values

mightyCelu 12 years ago
parent
commit
0579d18e76
2 changed files with 15 additions and 0 deletions
  1. 11 0
      Source/Engine/Math/Random.cpp
  2. 4 0
      Source/Engine/Math/Random.h

+ 11 - 0
Source/Engine/Math/Random.cpp

@@ -44,4 +44,15 @@ int Rand()
     return (randomSeed >> 16) & 32767;
 }
 
+float RandNormal(float meanValue, float variance)
+{
+	float val = 0.0f;
+	for(int i = 0; i < 12; i++) {
+		val += Rand() / 32768.0f;
+	}
+	val -= 6.0f;
+	// now val is approximatly standard normal distributed.
+	return val * sqrtf(variance) + meanValue;
+}
+
 }

+ 4 - 0
Source/Engine/Math/Random.h

@@ -24,6 +24,8 @@
 
 #include "Urho3D.h"
 
+#include <cmath>
+
 namespace Urho3D
 {
 
@@ -33,5 +35,7 @@ URHO3D_API void SetRandomSeed(unsigned seed);
 URHO3D_API unsigned GetRandomSeed();
 /// Return a random number between 0-32767. Should operate similarly to MSVC rand().
 URHO3D_API int Rand();
+/// Return a normal distributed number.
+URHO3D_API float RandNormal(float meanValue, float standardDeviation);
 
 }