001package algs12; 002import stdlib.*; 003/* *********************************************************************** 004 * Compilation: javac Flips.java 005 * Execution: java Flips N 006 * Dependencies: Counter.java StdRandom.java StdOut.java 007 * 008 * % java FLips 10 009 * 5 heads 010 * 5 tails 011 * delta: 0 012 * 013 * % java Flips 10 014 * 8 heads 015 * 2 tails 016 * delta: 6 017 * 018 * % java Flips 1000000 019 * 499710 heads 020 * 500290 tails 021 * delta: 580 022 * 023 *************************************************************************/ 024 025public class XFlips { 026 public static void main(String[] args) { 027 args = new String[] { "1000000" }; 028 029 int T = Integer.parseInt(args[0]); 030 Counter heads = new Counter("heads"); 031 Counter tails = new Counter("tails"); 032 for (int t = 0; t < T; t++) { 033 if (StdRandom.bernoulli(0.5)) heads.increment(); 034 else tails.increment(); 035 } 036 StdOut.println(heads); 037 StdOut.println(tails); 038 int delta = heads.tally() - tails.tally(); 039 StdOut.println("delta: " + Math.abs(delta)); 040 } 041}