/*
    Copyright (C) 2011 Brendon J. Brewer
    This file is part of DNest, the Diffusive Nested Sampler.

    DNest is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DNest is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DNest.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "RandomNumberGenerator.h"
#include <ctime>
#include <gsl/gsl_randist.h>
#include <cmath>

namespace DNest
{

	RandomNumberGenerator RandomNumberGenerator::instance(-time(0));

	RandomNumberGenerator::RandomNumberGenerator(int seed)
	{
		rng = gsl_rng_alloc(gsl_rng_taus);
		gsl_rng_set(rng, seed);	
	}

	RandomNumberGenerator::~RandomNumberGenerator()
	{
		gsl_rng_free(rng);
	}

	double RandomNumberGenerator::randomU()
	{
		return gsl_rng_uniform_pos(rng);
	}

	double RandomNumberGenerator::randn()
	{
		return gsl_ran_gaussian(rng, 1.0);
	}

	RandomNumberGenerator& RandomNumberGenerator::getInstance()
	{
		return instance;
	}

	double randomU()
	{
		return RandomNumberGenerator::getInstance().randomU();
	}

	double randn()
	{
		return RandomNumberGenerator::getInstance().randn();
	}

	int randInt(int numPossibilities)
	{
		return (int)floor(numPossibilities*randomU());
	}
}

