Thread: Coin Toss

  1. #1
    Registered User
    Join Date
    Aug 2004

    Coin Toss

    For a class I am taking, I have to write a program that looks at the out come of a coin toss for 1000, 10000, and 100000 tosses. I am also required to keep count of the longest sequence of heads or 1's and the longest sequence of alternating head and tails 1010101. heads = 1 tails = 0.
    i am having trouble with the latter, I need help on keeping track of the sequences. Here is my code so far:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    	int i, heads = 0, tails = 0, x;
    	for(i = 1; i < 1000; i++){
    		x = rand() % 2;
    		if(x == 1)
    		if(x == 0)
    	printf("%d heads, %d tails\n", heads, tails);
    	return 0;

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    >> I am also required to keep count of the longest sequence of heads or 1's

    when you encounter a head, increment 'count' and compare it to 'max' - if it's greater than 'max', set 'max' to 'count' and then 'count' to zero.

    >> and the longest sequence of alternating head and tails 1010101

    same as above, have a 'count' and 'max' variable to keep tally. create a 'last' variable to hold the value determined during the previous iteration. then, when you are inspecting the current toss, compare it with 'last' and, if they are different, do the same as above. if they are the same, just set 'count' to zero.

    >> for(i = 1; i < 1000; i++){

    that only does 999 iterations, btw...
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Make some counters for "last", "longest heads", "longest tails", and "current run". Track what the last coin flip was. If it's the same as it was last time, see what you need to update.

    Pesudocode would basicly give you the answer, so I won't post it. But with what I've suggested above, you should have it in no time.

    Curses! Foiled again.

    Hope is the first step on the road to disappointment.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    I wrote a program to do this just because it interested me. Here's the results I got so you can compare it to something when you've finished the program I won't show my code until you've figured it out since it's homework and all...
    itsme@dreams:~/C$ ./cointoss
    1000 tosses:
      max tails run: 9
      max heads run: 11
      max alternate: 10
    10000 tosses:
      max tails run: 11
      max heads run: 14
      max alternate: 12
    100000 tosses:
      max tails run: 18
      max heads run: 22
      max alternate: 17
    Of course, the numbers vary a bit between runs, but not much
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Quote Originally Posted by kazuakijp
    For a class I am taking, I have to write a program

    Well, since it's a programming class, I guess the first thing I should do is start programming.

    Let's see --- #include <stdio.h> --- void main() --- rats, it's int main() --- yada-yada-yada

    Now what?

    How about pretending it's not a programming assignment, but a coin flipping assignment. You are given a coin and locked in a room with a white board and a marker. The assignment: toss the coin lots of times and keep track of the longest sequence of heads. (After you learn how to do this, the same algorithm will undoubtedly be applicable to sequences of tails. Worry about the 101010 stuff later.)

    How would you do it? Well, since it's a coin-flipping assignment, better get to flipping the coin, right? Or, maybe, just maybe, you should map out a plan before you start flipping.

    How about this for a plan:

    1. Erase the board.

    2. Flip the coin

    2a. If it's tails, do nothing (we are only interested in heads right now).

    2b. If it's heads, make a mark on the board (this will be a tally of the number of heads in the sequence).

    3. Flip again.

    3a. If it's heads, make another mark

    3b. If it's tails, count the number of marks and write this number in a place on the board where it won't be disurbed by further counting. (This is where you will keep track of the maximum-length sequence of heads.) Now erase the tally marks so that you will be ready for the next time you get heads.

    4. Flip again.

    4a. If it's tails, do nothing.

    4b. If it's heads, make a tally mark

    Now you see the pattern. If only you had a computer, you can do the whole thing in a program loop:

    Before the loop begins, set a counter, say heads_counter, equal to zero. Set another counter, say max_heads_count, equal to zero.

    Now flip the coin repeatedly (Probably use rand(), or something.)

    Whenever you see heads, increment the heads_counter.

    Whenever you see tails, see if the value of heads_counter is greater than the value of max_heads_count. If it is, set max_heads_count equal to the value of heads_counter. In any case set the value of heads_counter back to zero.

    (Sound of repeated pounding on the door of the experiment room.)

    Hey, lemme outta here! I got some programming to do.

    Can you make a program now? I'll bet you can (assuming you know how to count and how to add in your programming language of choice).


    Last edited by Dave Evans; 08-31-2004 at 09:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. coin toss
    By ejd81882 in forum C Programming
    Replies: 3
    Last Post: 10-14-2008, 12:53 PM
  2. Another Coin Counting Program
    By MipZhaP in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2005, 02:02 AM
  3. coin toss program?
    By girliegti in forum C++ Programming
    Replies: 4
    Last Post: 09-17-2003, 10:09 AM
  4. Football - The AFC
    By Cshot in forum A Brief History of
    Replies: 4
    Last Post: 12-17-2002, 07:29 PM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM