import java.util.Random;

/** @author CPSC 111 Jan 2010 class
 ** @date Fri 5 March 2010
 * Flip a coin one million times and print out heads vs tails counts, using a while loop.
 */

public class CoinFlip {

	public static void main(String[] args) {
		int heads = 0;
		int tails = 0;
		int booleanHeads = 0;
		int booleanTails = 0;
		int counter = 0; // assumes test will be < limit
		// could have counter = 1, and then test for <= limit.
		int limit = 1000000; // not a constant, but also not magic number directly in the loop test.
		
		Random generator = new Random();
		while (counter < limit) {
			//rand = Random.nextBoolean(); // no: we want to use the Random *object*, not the Random class
			//Random generator = new Random(); // no: we want to make just one Random object and use it one million times. as opposed to making one million Random objects, using them once, and then throwing away.
			int value = generator.nextInt(2); // gives 0 or 1 as answer
			if (value == 1) {
				tails++;
				//counter++; // technically correct, but cleaner to have counter outside the conditional
			} else {
				heads++;
				//counter++;
			}
			
			boolean booleanValue = generator.nextBoolean();
			//if (booleanValue == true) { // this is correct, but code below is more concise
			if (booleanValue) {
					booleanHeads++;
			} else {
					booleanTails++;
			}
			
			counter++;
		}

		// we get the same results whether we use nextBoolean or nextInt, good!
		System.out.println("heads is "+heads+" tails is "+tails);
		System.out.println("BooleanHeads is "+booleanHeads+" booleanTails is "+booleanTails);
	}
}

