rng.normal

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

rng.normal

#1 Post 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.
<DarkGod> lets say it's intended

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: rng.normal

#2 Post by darkgod »

If you implement it I'll add it :)
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

yufra
Perspiring Physicist
Posts: 1332
Joined: Tue Jul 13, 2010 2:53 pm

Re: rng.normal

#3 Post 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;
}
<DarkGod> lets say it's intended

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: rng.normal

#4 Post by darkgod »

Looks good, taken for beta9
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: rng.normal

#5 Post by darkgod »

under the name rng.normalFloat
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning ;)

Post Reply