Question

"Flip a Coin!" You will write a program that allows the user to simulate a coin...

"Flip a Coin!"

You will write a program that allows the user to simulate a coin flip.

Note the following line of code: (Math.random()*2) +1;

This line generates a random number (a float) between 1 and 2. If you wanted to simulate somebody rolling a die, you could store the value of this variable as:

var coinFlip = (Math.random()*2) +1;

(Be sure to include all the parentheses in this line of code.)

Reminder - Of course, you’ll need to convert this to an integer…..

Have a button that says:   "Flip the Coin!"

If the coin flip is a 1, then output "Heads". If the coin flip is a 2, then output 'Tails".   Use a basic if-else block for this. (Do not use two separate if statements).  

You can output the result to an alert box.   

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Writing the code in C as given you need if else blocks in the progarm

#include<stdio.h>

#include<math.h>

int main()

{

int choice = (int) (Math.random() * 2) + 1;

// Math. random will generate random float value between 0 and 1, hence the function will return random float value between 0 and 2, when type casted to int, it will return either 0 or 1

if(choice==1)

printf("Heads\n");

window.alert(Heads);

else

printf("Tails\n");

window.alert(Tails);

return 0;

}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
"Flip a Coin!" You will write a program that allows the user to simulate a coin...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Simulate the flipping of a coin Print the result of the coin flip : use off...

    Simulate the flipping of a coin Print the result of the coin flip : use off numbers for Heads . and even numbers for Tails Update the number of times the result is Heads Update the number of times the coin has been flipped struct coin_prob{ int heads; int flips; }; void coin_flip(struct coin_prob * coin); You may use a rand functioon (assume its already been seeded), but no other pre-defined function

  • If you flip a coin and roll a die at the same time, what is the...

    If you flip a coin and roll a die at the same time, what is the probability of rolling a 5 or 6 and the coin showing heads? Question 3 Unanswered If you flip a coin and roll a die at the same time, what is the probability of rolling a 5 or 6 and the coin showing heads? (answer to 4 decimal places) Type your response

  • Consider the setting where you first roll a fair 6-sided die, and then you flip a...

    Consider the setting where you first roll a fair 6-sided die, and then you flip a fair coin the number of times shown by the die. Let D refer to the outcome of the die roll (i.e., number of coin flips) and let H refer to the number of heads observed after D coin flips. (a) Suppose the outcome of rolling the fair 6-sided die is d. Determine E[H|d] and Var(H|d). (b) Determine E[H] and Var(H).

  • You flip a fair coin. On heads, you roll two six-sided dice. On tails, you roll...

    You flip a fair coin. On heads, you roll two six-sided dice. On tails, you roll one six-sided dice. What is the chance that you roll a 4? (If you rolled two dice, rolling a 4 means the sum of the dice is 4) O 1 2 3 36 1 2 1 6 + + 1 4 36 1 6 2 2 1 36 + -10 2 . 4 36 + 4 6 2 2

  • Write a function that flips a coin; it returns heads or tails. Use that function to...

    Write a function that flips a coin; it returns heads or tails. Use that function to write a GetHeadsInARow functions that accepts the number of heads to roll in a row before you return with total rolls it took to get that number of heads in a row. Please Code in Python3 my attempt below will not print out the answer and I do not know why def HeadTails(): from random import randrange """Will simulate the flip of a coin."""...

  • You roll a 6-sided die. What is the probability that you will roll either a 3 or a 2? P (3 or 2) = You flip a 2-sided co...

    You roll a 6-sided die. What is the probability that you will roll either a 3 or a 2? P (3 or 2) = You flip a 2-sided coin. What is the probability that you will get either heads or tails? P (heads or tails) =

  • Suppose you flip a fair coin repeatedly until you see a Heads followed by another Heads...

    Suppose you flip a fair coin repeatedly until you see a Heads followed by another Heads or a Tails followed by another Tails (i.e. until you see the pattern HH or TT). (a)What is the expected number of flips you need to make? (b)Suppose you repeat the above with a weighted coin that has probability of landing Heads equal to p.Show that the expected number of flips you need is 2+p(1−p)/1−p(1−p)

  • In C++ please Create a coin-flipping game. Ask the user how many times to flip the...

    In C++ please Create a coin-flipping game. Ask the user how many times to flip the coin, and use the random function to determine heads or tails each time a coin is flipped. Assume the user starts with $50. Every time the coin is flipped calculate the total (heads +$10, tails -$10). Create another function to test if the user has gone broke yet (THIS FUNCTION MUST RETURN A BOOLEAN TRUE/FALSE VALUE). End the program when the user is broke...

  • Need help understanding this question from CodeStepByStep in Java, any help is appreciated! Write a method...

    Need help understanding this question from CodeStepByStep in Java, any help is appreciated! Write a method named coinFlip that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of whitespace-separated tokens. Assume that the input file data represents results of sets of coin flips. A coin flip is either the letter H or T, or the word Heads or Tails, in either upper or lower case, separated by at...

  • Write a C program that prompts the user for the number for times the user wants...

    Write a C program that prompts the user for the number for times the user wants to flip a coin. Your program will flip a virtual coin that number of times and return the following: 1) The number of times heads occurred. 2) The number of times tails occured. Then take the coin flip program and modify the random number generator to roll a single 6-sided dice.   DO NOT DISPLAY THE RESULTS OF EACH ROLL. 1) Ask the user how...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT