Final Assignemnt
Code
/// Name: Daniel Nguyen
/// Period: 6
/// Program Name: probprob Program
/// File Name: probprob.java
/// Date Finished: 9/9/2015
import java.util.Random;
import java.util.Scanner;
public class probprob
{
public static void main( String[] args )
{
Random rng = new Random();
Scanner keyboard = new Scanner(System.in);
int want = 1;
int next = 0;
int flips = 0;
int flip = 0;
int heads = 0;
int tails = 0;
while (flips != want) {
while (next != 1) {
System.out.println("Enter how many times you want to flip a coin: ");
System.out.print("> ");
want = keyboard.nextInt();
if (want < 0 || want > 2100000000 )
System.out.println("Sorry, pick a number between 0 and 2,100,000,000");
else if (want > 0 || want < 2100000000) {
System.out.println("Ok! " + want + " times WOW! Give me a sec.");
next = 1;
}
}
flip = rng.nextInt(2);
if (flip == 1)
heads++;
else if (flip == 0)
tails ++;
flips++;
}
double probOfHeads = (double)heads / want;
double probOfTails = 1 - probOfHeads;
probOfHeads = probOfHeads * 100;
probOfTails = probOfTails * 100;
System.out.println("You rolled " + heads + " heads, and " + tails + " tails.");
System.out.println("The probability of rolling heads was " + probOfHeads + "% and the probability of rolling tails was " + probOfTails + "%.");
}
}
// The best number of flips to get ~50% consistantly is 6000 flips
Picture of the output