Thread: Coinflip Simulation

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    6

    Post Coinflip Simulation

    Hello Guys,
    i have an exercise to do in C but im relly bad in C, so i need your help to solve this exercise.
    Thats the exercise:
    Exercise 84: Coin Flip Simulation
    Whatís the minimum number of times you have to flip a coin before you can have three consecutive flips that result in the same outcome (either all three are heads or all three are tails)? Whatís the maximum number of flips that might be needed? How many flips are needed on average? In this exercise we will explore these questions by creating a program that simulates several series of coin flips. Create a program that uses Pythonís random number generator to simulate flipping a coin several times. The simulated coin should be fair, meaning that the probability of heads is equal to the probability of tails. Your program should flip simulated coins until either 3 consecutive heads of 3 consecutive tails occur. Display an H each time the outcome is heads, and a T each time the outcome is tails, with all of the outcomes for one simulation on the same line. Then display the number of flips that were needed to reach 3 consecutive occurrences of the same outcome. When your program is run it should perform the simulation 10 times and report the average number of flips needed. Sample output is shown below:

    Output:
    H T T T (4 flips)

    H H T T H T H T T H H T H T T H T T T (19 flips)
    T T T (3 flips)
    T H H H (4 flips)
    H H H (3 flips)
    T H T T H T H H T T H H T H T H H H (18 flips)
    H T T H H H (6 flips)
    T H T T T (5 flips)
    T T H T T H T H T H H H (12 flips)
    T H T T T (5 flips)
    On average, 7.9 flips were needed.
    Can someone helps me?


  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,102
    Show your attempt.

    The first two questions are rather simple-mineded. Obviously the minimum is 3, and the maximum is, what, a gazillion? It's a stupid question.
    Last edited by john.c; 01-08-2021 at 11:14 AM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    6
    Yeah I know a stupid Question but i have to solve it...

    I think the min. and max. can you write in your program like this:
    H T T T (4 flips)
    H H T T H T H T T H H T H T T H T T T (19 flips)
    T T T (3 flips)
    T H H H (4 flips)
    H H H (3 flips)
    T H T T H T H H T T H H T H T H H H (18 flips)
    H T T H H H (6 flips)
    T H T T T (5 flips)
    T T H T T H T H T H H H (12 flips)
    T H T T T (5 flips)
    On average, 7.9 flips were needed.
    Min.: 3 flips
    Max.: 19 flips



    But i have no Idea HOW...

    My attempt is relly bad and dont work like I want to work, thats why i need help on this project...

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    804
    i have an exercise to do in C but im relly bad in C,
    ...
    Create a program that uses Pythonís random number generator to simulate flipping a coin several times.
    Bad at spelling also!

    This is a C forum. The instructions say to use the Python RNG, rather than the C version. Why do you think you should be coding the program in C!

  5. #5
    Registered User
    Join Date
    Jan 2021
    Posts
    6
    Yeah sorry for my spelling im bad in english too XD.

    So my teacher/Master sad that we have to do it in C cause we cant programming Phyton

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    804
    Quote Originally Posted by FakePlays View Post
    Yeah sorry for my spelling im bad in english too XD.

    So my teacher/Master sad that we have to do it in C cause we cant programming Phyton
    Programmers invented spell checker programs! ;^)

    Then why specify using the Python RNG???

    Post your code if have some problem with your code, and ask specific questions.

  7. #7
    Registered User
    Join Date
    Jan 2021
    Posts
    6
    My specific question is how can I solve this exercise
    What’s the minimum number of times you have to flip a coin before you can have three consecutive flips that result in the same outcome (either all three are heads or all three are tails)? What’s the maximum number of flips that might be needed? How many flips are needed on average? In this exercise we will explore these questions by creating a program that simulates several series of coin flips. The simulated coin should be fair, meaning that the probability of heads is equal to the probability of tails. Your program should flip simulated coins until either 3 consecutive heads of 3 consecutive tails occur. Display an H each time the outcome is heads, and a T each time the outcome is tails, with all of the outcomes for one simulation on the same line. Then display the number of flips that were needed to reach 3 consecutive occurrences of the same outcome.


    ​My code is pure trash and dont work...

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    804
    Quote Originally Posted by FakePlays View Post
    My specific question is how can I solve this exercise
    My code is pure trash and dont work...
    So the output you displayed above is what you expect? What is the actual output from your code?

    We can't program the code for you. You have to make an attempt, and post your code so we can comment on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ and Simulation
    By CNewProg in forum C++ Programming
    Replies: 5
    Last Post: 09-27-2016, 04:04 AM
  2. making a simulation in c++
    By chaklader in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2012, 04:37 AM
  3. Simulation - M(RT)^2 method
    By wu_weidong in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 01:47 PM
  4. Simulation
    By CodeJerk in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2002, 10:56 PM
  5. spy++ simulation
    By bennyandthejets in forum Windows Programming
    Replies: 1
    Last Post: 11-20-2002, 09:15 AM

Tags for this Thread