/*
 * Created on Apr 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ps6;
import java.util.Random;

/**
 * @author knorton
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Message {
	private static Random		rand = new Random();
	private double[]			p = new double[2];
	
	public Message() {
		this.p[0] = rand.nextDouble();
		this.p[1] = rand.nextDouble();
		this.normalize();
	}
	
	public Message(double p0, double p1) {
		this.p[0] = p0;
		this.p[1] = p1;
		this.normalize();
	}
	
	public Message(int p0, int p1) {
		this.p[0] = (double)p0;
		this.p[1] = (double)p1;
		this.normalize();
	}
	
	public Message(Message m) {
		this.p[0] = m.getP(0);
		this.p[1] = m.getP(1);
	}
	
	public double getP(int symbol) {
		return this.p[symbol];
	}
	
	public void setP(int symbol, double prob) {
		this.p[symbol] = prob;
	}
	
	public static Message normalize(Message m) {
		Message mc = new Message(m);
		mc.normalize();
		return mc;
	}
	
	public void normalize() {
		this.p[0] = this.p[0]/(this.p[0] + this.p[1]);
		this.p[1] = 1.0 - this.p[0];
	}
	
	public String toString() {
		return "(" + this.p[0] + "," + this.p[1] + ")";
	}
}
