/**
 *  Content:   RandomManager.java
 *  Author:    Jakob Svensson (c) 2008
 */

package otherSupport;

import cern.jet.random.Normal;
import cern.jet.random.engine.RandomEngine;

/**
 * A class to make it possible to control the random number generation globally
 * in the COLT package. 
 *
 */
public class RandomManager {
	
	static RandomManager instance = null;
	public final static double LOGZERO = Double.NEGATIVE_INFINITY;
	
	RandomEngine rng;
	cern.jet.random.Uniform uniform;
	cern.jet.random.Normal normal;
	
	
	public RandomManager() {
		setRandomEngine(new cern.jet.random.engine.MersenneTwister((int) System.currentTimeMillis()));		
	}
	
	public void setSeed(int seed) {	
		setRandomEngine(new cern.jet.random.engine.MersenneTwister(seed));		
	}
	
	public static RandomManager instance() {
		if (instance == null) {
			instance = new RandomManager();
		}
		return instance;
	}
	
	public void setRandomEngine(RandomEngine rng) {
		this.rng = rng;

		uniform = new cern.jet.random.Uniform(rng);
		normal = new cern.jet.random.Normal(0,1,rng);
	}
	
	public RandomEngine getRandomNumberGenerator() {
		return rng;
	}
	
	public double nextNormal(double mean, double standardDeviation) {
		return normal.nextDouble(mean, standardDeviation);
	}
	
	public double nextUniform(double from, double to) {
		return uniform.nextDoubleFromTo(from, to);
	}
	
	public static void main(String[] args) {
		RandomEngine rnd = new cern.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
		cern.jet.random.Normal normalGen = new Normal(0, 1, rnd);
		for(int i=0; i < 100; i++){
			System.out.println(
					normalGen.nextDouble(0, 1) +"\t"+
					normalGen.nextDouble(0, 0.0001));
					
		}
	}
}
