Browse Source

Merge remote-tracking branch 'remotes/mightycelu/normalDistr'

Lasse Öörni 12 years ago
parent
commit
72de756ce3

+ 1 - 0
Source/Engine/LuaScript/pkgs/Math/MathDefs.pkg

@@ -34,5 +34,6 @@ bool Equals(float lhs, float rhs);
 float Random();
 float Random();
 float Random(float range);
 float Random(float range);
 float Random(float min, float max);
 float Random(float min, float max);
+float RandomNormal(float meanValue, float variance);
 int Random @ RandomInt(int range);
 int Random @ RandomInt(int range);
 int Random @ RandomInt(int min, int max);
 int Random @ RandomInt(int min, int max);

+ 1 - 0
Source/Engine/LuaScript/pkgs/Math/Random.pkg

@@ -3,3 +3,4 @@ $#include "Random.h"
 void SetRandomSeed(unsigned seed);
 void SetRandomSeed(unsigned seed);
 unsigned GetRandomSeed();
 unsigned GetRandomSeed();
 int Rand();
 int Rand();
+float RandStandardNormal();

+ 2 - 0
Source/Engine/Math/MathDefs.h

@@ -151,5 +151,7 @@ inline float Random(float min, float max) { return Rand() * (max - min) / 32767.
 inline int Random(int range) { return (Rand() * (range - 1) + 16384) / 32767; }
 inline int Random(int range) { return (Rand() * (range - 1) + 16384) / 32767; }
 /// Return a random integer between min and max - 1.
 /// Return a random integer between min and max - 1.
 inline int Random(int min, int max) { return (Rand() * (max - min - 1) + 16384) / 32767 + min; }
 inline int Random(int min, int max) { return (Rand() * (max - min - 1) + 16384) / 32767 + min; }
+/// Return a random normal distributed number with the given mean value and variance.
+inline float RandomNormal(float meanValue, float variance) { return RandStandardNormal() * sqrtf(variance) + meanValue; }
 
 
 }
 }

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

@@ -44,4 +44,15 @@ int Rand()
     return (randomSeed >> 16) & 32767;
     return (randomSeed >> 16) & 32767;
 }
 }
 
 
+float RandStandardNormal()
+{
+	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;
+}
+
 }
 }

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

@@ -33,5 +33,7 @@ URHO3D_API void SetRandomSeed(unsigned seed);
 URHO3D_API unsigned GetRandomSeed();
 URHO3D_API unsigned GetRandomSeed();
 /// Return a random number between 0-32767. Should operate similarly to MSVC rand().
 /// Return a random number between 0-32767. Should operate similarly to MSVC rand().
 URHO3D_API int Rand();
 URHO3D_API int Rand();
+/// Return a standard normal distributed number.
+URHO3D_API float RandStandardNormal();
 
 
 }
 }

+ 1 - 0
Source/Engine/Script/MathAPI.cpp

@@ -82,6 +82,7 @@ static void RegisterMathFunctions(asIScriptEngine* engine)
     engine->RegisterGlobalFunction("int RandomInt()", asFUNCTION(Rand), asCALL_CDECL);
     engine->RegisterGlobalFunction("int RandomInt()", asFUNCTION(Rand), asCALL_CDECL);
     engine->RegisterGlobalFunction("int RandomInt(int)", asFUNCTIONPR(Random, (int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("int RandomInt(int)", asFUNCTIONPR(Random, (int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("int RandomInt(int, int)", asFUNCTIONPR(Random, (int, int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("int RandomInt(int, int)", asFUNCTIONPR(Random, (int, int), int), asCALL_CDECL);
+	engine->RegisterGlobalFunction("float RandomNormal(float, float)", asFUNCTIONPR(RandomNormal, (float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("void SetRandomSeed(uint)", asFUNCTION(SetRandomSeed), asCALL_CDECL);
     engine->RegisterGlobalFunction("void SetRandomSeed(uint)", asFUNCTION(SetRandomSeed), asCALL_CDECL);
     engine->RegisterGlobalFunction("uint GetRandomSeed()", asFUNCTION(GetRandomSeed), asCALL_CDECL);
     engine->RegisterGlobalFunction("uint GetRandomSeed()", asFUNCTION(GetRandomSeed), asCALL_CDECL);
 }
 }