# Semester 1 Final Program: Coin Flip Probablity

## Code

```
///name: Nick Cerdan
///Period: 6
///program name: Semester 1 Final
///file name: ProbGenerator.java
///date finished; 1/22/16

import java.util.Scanner;
import java.util.Random;

public class ProbGenerator
{
public static void main(String[] args)
{
int heads, tails; // I named these 'heads' and 'tails' so they were easy to keep track of, and I made them ints
heads = 0;        // because they would always be whole numbers, so there was no need to make them doubles.
tails = 0;

Scanner kb = new Scanner(System.in);
Random r = new Random();

System.out.println("");
System.out.println("Semester 1 Final Program!");
System.out.print("How many time would you like to flip the coin? " );
int flips = kb.nextInt(); // I made 'flips' an int bc it would always be a whole number, not a decimal.
while (flips < 1 || flips > 2100000000) // I used a while loop to repeat this section of code
{                                       // until the user inputs an appropriate value, however many
if (flips < 1)                      // times tht may take.
{
System.out.print("You must choose a number greater than 1. Choose again: ");
flips = kb.nextInt();
}

else
{
System.out.print("You must choose a number less than 2.1 billion. Choose again: ");
flips = kb.nextInt();
}
}

for (int n = 0; n < flips; n++) // I used a for loop because it's the most efficient way to repeat
{                               // a section of code for a definite amount of times.
int side = r.nextInt(2);    // I made 'side' an int because I only needed 2 values, 1 and 2.

if (side == 1)
else
tails++;
}

System.out.println("");
System.out.println("and " + tails + " tails.");
System.out.println("");

double ProbHeads = ((double)heads / flips) * 100; // I made the probablities doubles because they would most
double ProbTails = ((double)tails / flips) * 100; // likely not be whole numbers and an int wouldn't justly
// represent the true probality due to round off.
System.out.println("and the probability of flipping tails was " + ProbTails + "%.");
}
}

/* I found that the largest number of flips possible (2.1 billion) would consistently generate the closest to 50% of each side. The fewer number fo flips I would do the more likely the percentages were to flunctuate away from the middle.

*/ ```