Simulate the tossing of a coin three times and print out the percentage of cases in which one gets three tails

5.6k views Asked by At

Attached is the problem: http://puu.sh/42QtI/ea955e5bef.png

In terms of code, this is what I have so far

The question asks to "calculate the simulated percentage of three tails," which is the part I am stuck on. Could someone give me some insight on what to progress next?

public static boolean isThreeTails(){
            Random rand = new Random();
            int numberOfTosses = 3;
            int numberOfHeads = 0;
            int numberOfTails = 0;

            for(int i = 1; i <= numberOfTosses; i++){
                int value = rand.nextInt(2);
                if(value == 0){
                    numberOfTails++;
                }else{
                    numberOfHeads++;
                }
            }
            if(numberOfTails == 3){
                return true;
            }
            else{
                return false;
            }
        }

double numTosses = 1000000; //choose whatever here
        double threeTails = 0;
        for(int i =0; i < numTosses; i++){
            if(isThreeTails()){
                 threeTails++;
            }
        }
        System.out.println("Theoretical probability of 3 Tails: " + (double) 1/8);
        System.out.println("Actual results for " + numTosses + " tosses = " + threeTails/numTosses);

EDIT: Here, I am creating a counter for when there are triple tails. It would increment the numberOfTripleTails counter. If it rolls a "H", the numberOfTails would simply go back to zero. However, my code seems to only give '3' as an answer.

EDIT 2: Done!

3

There are 3 answers

1
mistahenry On BEST ANSWER

The method that you have already written simulates three tosses. I've modified that method so that it is now a callable function isThreeTails()

public static boolean isThreeTails(){
    Random rand = new Random();
    int numberOfTosses = 3;
    int numberOfHeads = 0;
    int numberOfTails = 0;

    for(int i = 1; i <= numberOfTosses; i++){
        int value = rand.nextInt(2);
        if(value == 0){
            numberOfTails++;
        }else{
            numberOfHeads++;
        }
    }
    if(numberOfTails == 3){
        return true;
    }
    else{
        return false;
    }
}

Now you will want to call this method from the main method of ThreeTosses.java

double numTosses = 100; //choose whatever here
double threeTails = 0;
for(int i =0; i < numTosses; i++){
    if(isThreeTails()){
         threeTails++;
    }
}
System.out.println("Theoretical probability of 3 Tails: " + (double) 1/8);
System.out.println("Actual results for " + numTosses + " tosses = " + threeTails/numTosses);
3
Surveon On

Alright - you've run your simulation and you have your value for number of heads and number of tails. Now you'll need to run a few more.

Each time you run a simulation, increment a variable that tracks the total amount of times you've run it. If number of tails comes out to three, you increment another variable: let's call it successes.

The outcome to the problem are the successes over the total times the simulation was run.

0
Daniel Kaplan On

The question is saying, "in theory, you should get 3 tails 1/8th of the time". Now it's saying, "OK, you know the theory, now actually do this on a computer and see what you really get."

What you want to do is run this code a bunch of times and keep track of the number of times you got 3 tails. Take that number and divide it by the total number of times you ran the code. That should be the simulated percentage.

Just in case you can't tell, I'm saying to do this in code, not by manually running your current code over and over again. Here's some pseudo code:

threeTailsCount = 0
for i = 0; i < 1000; i++
  if currentCodeReturns3Tails
    threeTailsCount += 1;

print (threeTailsCount / 1000)