Simulate the tossing of a coin three times and print out the percentage of
cases in which one gets three tails
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?
Random rand = new Random(); // new random object
int numberOfTosses = 100000; // Initializing the number of tosses
int numberOfTails = 0; // Initializing the number of tails
int numberOfTripleTails = 0;
char tails;
for(int i = 1; i <= numberOfTosses; i++){
while(numberOfTails<=3){
tails = toss(rand);
if(tails == 'T'){
numberOfTails++;
if(numberOfTails == 3){
numberOfTripleTails++;
}
}else{
numberOfTails = 0;
}
}
}
System.out.println(numberOfTripleTails);
System.out.println("Theoretical probability of 3 Tails: 1/8"); //
theoretical probability
}
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.
No comments:
Post a Comment