Page 1 of 1
rng.normal
Posted: Wed Aug 11, 2010 5:08 pm
by yufra
I want to have damage in VR handled as a normal distribution and decided to see if T-Engine already had support for random numbers from normal distributions. I found rng.normal, or more specifically "static int rng_normal(lua_State *L)". Do you have any plans to support a floating point version? If I implement one would you add it to T-Engine? I have no experience in random number generators so it might take a while, but I understand if this isn't very high on your priority list. For now I am using rng.normal and it works, but is just discretized and looks a bit funny at times.
Re: rng.normal
Posted: Wed Aug 11, 2010 5:25 pm
by darkgod
If you implement it I'll add it

Re: rng.normal
Posted: Wed Aug 11, 2010 8:22 pm
by yufra
Well my first pass appears to work, with a histogram of 10,000 values looking normal (hehe). I added this function to rnglib under the name "normal_float". The Box-Muller transform creates pairs of values, so I am currently saving the extra one and supplying the saved value every other call. I have very little C experience, so if it is faster to drop the static boolean checking and simply throw away the z1 random number I can change it. Any comments or suggestions?
Code: Select all
/*
* Generate a random floating-point number of NORMAL distribution
*
* Uses the Box-Muller transform.
*
*/
static int rng_normal_float(lua_State *L)
{
static const double TWOPI = 6.2831853071795862;
static bool stored = FALSE;
static double z0;
static double z1;
double mean = luaL_checknumber(L, 1);
double std = luaL_checknumber(L, 2);
double u1;
double u2;
if (stored == FALSE)
{
u1 = genrand_real1();
u2 = genrand_real1();
u1 = sqrt(-2 * log(u1));
z0 = u1 * cos(TWOPI * u2);
z1 = u1 * sin(TWOPI * u2);
lua_pushnumber(L, (z0*std)+mean);
stored = TRUE;
}
else
{
lua_pushnumber(L, (z1*std)+mean);
stored = FALSE;
}
return 1;
}
Re: rng.normal
Posted: Wed Aug 11, 2010 10:15 pm
by darkgod
Looks good, taken for beta9
Re: rng.normal
Posted: Wed Aug 11, 2010 10:16 pm
by darkgod
under the name rng.normalFloat