Thread: Coinflip Simulation

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

    Coinflip Simulation

    Hello Guys, I need help to my program on C.
    I want to create a Coinflip Simulation that count how often a coin to need flipp until 3 times the same (head or tale) in a row.
    Thats what i had already:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int kopf = 1; // "kopf" is head
    int zahl = 0; // "zahl" is tale
    int variable;
    int ergebnis = 0;
    int i;
    
    int main() {
        time_t t;
        srand(time(&t));
        for(i = 0; i < 10; i++) {
            ergebnis = ("%d ", rand() % 2);
    
            if (ergebnis == 1) {
                printf("K "); //"K" is for head
            }
    
            if (ergebnis == 0) {
                printf("Z "); //"Z" is for tale
            }
        }
    
    }
    
    
    I think the black 10 have to exchange with a variable thats know exectly when 3 times are tail or 3 time are head.
    But i dont know how can i make this.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    502
    Quote Originally Posted by FakePlays View Post
    Hello Guys, I need help to my program on C.
    I want to create a Coinflip Simulation that count how often a coin to need flipp until 3 times the same (head or tale) in a row.
    You need a buffer of three results. The easiest way to do this
    is via a small array

    char results[3];

    initialise to some value which is neither heads nor tails, because you must toss the coin at least three times.

    Then on each toss, add the new value to the buffer. This involves moving the oldest value out. Since you have only three values, you can easily hard-code this.

    Then test whether all three results are the same.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


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

    -

    Quote Originally Posted by Malcolm McLean View Post
    You need a buffer of three results. The easiest way to do this
    is via a small array

    char results[3];

    initialise to some value which is neither heads nor tails, because you must toss the coin at least three times.

    Then on each toss, add the new value to the buffer. This involves moving the oldest value out. Since you have only three values, you can easily hard-code this.

    Then test whether all three results are the same.
    Can you send me the code thats already included to my code?
    That were really awesome

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,126
    No he can't send you the code. You have to do it yourself.

    Another solution is a count of repeated symbols. You just need a "last_flip" value initialized to an unused value (e.g., -1), and a count variable. count should start at 1 (since there is always "one in a row") and the count should be incremented anytime the current flip equals the last flip.

    BTW, does anyone know how to calculate the average mathematically? It seems that the exact answer for the average of 3-in-a-row is 7, which is 2**3 - 1. In a simulation from 1-in-a-row to 10-in-a-row I get the following results, which are in line with the formula 2^n - 1.
    Code:
    min    max    average
      1      1       1.00
      2     22       3.00
      3     59       7.02
      4    188      15.02
      5    375      31.03
      6    820      62.93
      7   1537     127.06
      8   3549     254.79
      9   7086     510.40
     10  14034    1022.82
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coinflip Simulation
    By FakePlays in forum C Programming
    Replies: 7
    Last Post: 01-08-2021, 11:48 AM
  2. C++ and Simulation
    By CNewProg in forum C++ Programming
    Replies: 5
    Last Post: 09-27-2016, 04:04 AM
  3. making a simulation in c++
    By chaklader in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2012, 04:37 AM
  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