فهرست منبع

Added scriptbindings for normal distribution generator and included the generator in the mathdefs

mightyCelu 12 سال پیش
والد
کامیت
03888602bf

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

@@ -34,5 +34,6 @@ bool Equals(float lhs, float rhs);
 float Random();
 float Random(float range);
 float Random(float min, float max);
+float RandomNormal(float meanValue, float variance);
 int Random @ RandomInt(int range);
 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);
 unsigned GetRandomSeed();
 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; }
 /// Return a random integer between min and max - 1.
 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; }
 
 }

+ 2 - 2
Source/Engine/Math/Random.cpp

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

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

@@ -24,8 +24,6 @@
 
 #include "Urho3D.h"
 
-#include <cmath>
-
 namespace Urho3D
 {
 
@@ -35,7 +33,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);
+/// 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(int)", asFUNCTIONPR(Random, (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("uint GetRandomSeed()", asFUNCTION(GetRandomSeed), asCALL_CDECL);
 }