# Thread: Coinflip Simulation

1. ## 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. Originally Posted by FakePlays 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;

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. 3. ## - Originally Posted by Malcolm McLean You need a buffer of three results. The easiest way to do this
is via a small array

char results;

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. 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``` Popular pages Recent additions #### Tags for this Thread

prograaming, program, program c, script, simulation 