/****************************************************************************************


	MCGaussianRandom.h

	Copyright: (c) 2001 Eovia Corp. All rights reserved.

	Author:		Benjamin
	Date:		7/25/02


*****************************************************************************************/

#ifndef __MCGAUSSRANDOM__
#define __MCGAUSSRANDOM__

#include "MCBasicTypes.h"
#include "MCBasicDefines.h"
#include "MCCountedObject.h"
#include "MCRandom.h"
#include "math.h"


/// \addtogroup MCCore
/// \{

/// Gaussian Random Number Generator
///
class TMCGaussianRandom : public TMCRandom, public TMCCountedObject
{
public:
	static void Create(TMCGaussianRandom** gen, int32 seed)
	{
		TMCCountedCreateHelper<TMCGaussianRandom> result(gen);
		result = new TMCGaussianRandom(seed);
		MCVerify(*gen);
	}

	real32 GaussianRand()
	{	
		real32 u;
		do
		{
			u = RealRand();
		} while (u == 0);
		return sqrt(-2.0 * log(u)) * cos (TWO_PI * RealRand());
	}

	real32 GaussianRandom(real32 eps, real32 var)
	{	
		if (eps == 0.0f || var == 0.0f) return 0.0f;
		real32 u;
		real32 s2 = tan(PI2 * eps);
		s2 *=s2;
		do
		{
			u = RealRand();
		} while (u == 0);
		u = u + (1 - u) * exp(- var * var / (2 * s2));
		return sqrt(-2.0 * s2 * log(u)) * cos (TWO_PI * RealRand());
	}
protected:

	TMCGaussianRandom()			 {}
	TMCGaussianRandom(int32 seed) : TMCRandom(seed)
	{
	}

};

/// \}

#endif