A coin is tossed repeatedly, and a payoff of 2n dollars is made, where n is the
ID: 673347 • Letter: A
Question
A coin is tossed repeatedly, and a payoff of 2n dollars is made, where n is the number of the toss on which the first Head appears. So TTH pays $8, TH pays $4 and H pays $2. Write a program to simulate playing the game 10 times. Display the result of the tosses and the payoff. At the end, display the average payoff for the games played. A typical run would be:
TTTH You win $16.00
TH You win $4.00
TH You win $4.00
H You win $2.00
TTTTH You win $32.00
TH You win $4.00
H You win $2.00
H You win $2.00
TH You win $4.00
TH You win $4.00
The average payout was $7.40
Explanation / Answer
The logic is same in any language. You did not specified language so I wrote it in java.
import java.util.Random;
import java.util.Scanner;
public class TossPayout {
public static void main(String []args){
Random rand = new Random();
int total = 0;
//simulating tossing for 10 times
for(int i = 0; i < 10; i++) {
//In each iteration coin is tossed till head turns up
int count = 0;
String output ="";
int num = -1;
do {
num = rand.nextInt();
//System.out.println(" count = "+count+", num = "+num);
count++;
//Generating a random number, if it is divisble by 2 its Head, otherwise its tail
if(num % 2 == 0) {
//payoff = 2n dollars is made, where n is the number of the toss on which the first Head appears
total = total + 2 * count;
output += "H You Won $ "+(2 * count);
System.out.println(output);
break;
} else {
output += "T";
}
} while(num % 2 != 0);
}
System.out.println("The average payout is $ "+ (float) total/10.0);
}
}
-----------------------------output--------------------------
H You Won $ 2
TTH You Won $ 6
TTH You Won $ 6
H You Won $ 2
TTTTH You Won $ 10
H You Won $ 2
TTTTTTH You Won $ 14
H You Won $ 2
H You Won $ 2
H You Won $ 2
The average payout is $ 4.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.