C++ coin toss simulation program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PAK11
    New Member
    • Mar 2008
    • 11

    C++ coin toss simulation program

    This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result should be return to the main program. I'm having trouble figuring it out, can anyone please help?


    [code=cpp]
    #include <iostream>
    using namespace std;
    # include <ctime>

    int coin();

    int main ()
    {
    int NUM_FLIPS = 100;
    int count, face, heads = 0, tails = 0;

    // initialize the random number generator
    srand(static_ca st<int>(time(0) ));

    // generate and count the number of heads and tails
    for (int count=1; count <= NUM_FLIPS; count++)
    {
    face = coin();
    if (face == 1)
    heads++;
    else
    tails++;
    cout << face << endl;
    }

    cout << "The number flips: " << NUM_FLIPS << endl;
    cout << "The number of heads: " << heads << endl;
    cout << "The number of tails: " << tails << endl;
    }[/code]
    Last edited by sicarie; Apr 11 '08, 06:00 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are not using the random number.

    Also, I see the coin() function returns 1 for heads but I don't see the coin() function itself to see how it works.

    Otherwise, what you have looks pretty much OK - maybe except for that static_cast. Try to avoid all casting in C++.

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      .I need to add a function named coin to simulate a coin toss
      Create the function.
      In the function use the rand() function to generate a random number.
      Then use the modulus operator (%) to limit the result to 2 values.
      use an if statement to see if the result is heads or tails
      return the result.

      Comment

      • PAK11
        New Member
        • Mar 2008
        • 11

        #4
        Well I am running what I have now and it builds but I can't seem to figure out how to get the program to run through more than once. A number is generated but the value does not change. I think the random number generator is only going through one time and then that value is repeated. What should I do?

        [code=cpp]
        #include <iostream.h>
        using namespace std;
        #include <ctime>

        int coin();

        int main ()
        {
        int NUM_FLIPS = 100;
        int count, face, heads = 0, tails = 0;
        face==1 + rand()%(2-1+1);
        // generate and count the number of heads and tails
        for (int count=1; count <= NUM_FLIPS; count++)
        {
        if (face == 1)
        {
        cout<<"It is Heads";
        heads=heads+1;
        }
        else
        cout<<"It is Tails";
        tails=tails+1;
        }
        cout << "The number flips: " << NUM_FLIPS << endl;
        cout << "The number of heads: " << heads << endl;
        cout << "The number of tails: " << tails << endl;

        return 0;
        }[/code]
        Last edited by sicarie; Apr 11 '08, 07:07 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Random numbers are not actually random in computing - they use a 'seed' value to get started, and go through a very complex mathematical formula to make sure that the overall distribution of numbers is equal between odd, even, positive, negative, etc... The key is that they are repeatable - so you can have the same seed value and get the same numbers from rand() every time. This is useful in scientific experiments. If you want to add a greater layer of randomization to the numbers, you can set the seed to the current time (which is accurate to the millisecond).

          To loop through this mroe than once, you need either a for loop or a while loop around the part you want to loop through.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            You had the srand function in your original code, which is correct to seed the random number generator (as sicarie explained). WFC suggested that you not use static_cast<int >, but you definitely need to use the srand function. Try calling it like this:

            srand(time(0));

            Comment

            • PAK11
              New Member
              • Mar 2008
              • 11

              #7
              Thank you everybody so much for the help, but I still get get it working right. Below is how I have it, it run through the program but evrytime it just says that i got tails 100 times and does not include both heads and tails.


              [CODE=cpp]#include <iostream>
              using namespace std;
              #include <ctime>
              int coin();
              int main()
              {
              int NUM_FLIPS = 100;
              int count, face, heads = 0, tails = 0;
              int random=1 && 2;

              // Initialize the Random Number Generator
              srand(static_ca st<int>(time(0) ));

              // Generate and Count the Number of Heads and Tails
              for (int count=1; count <= NUM_FLIPS; count++)
              {

              face = random;
              if (face == 1)
              heads++;
              else
              tails++;
              cout << face << endl;
              }

              cout << "The number flips: " << NUM_FLIPS << endl;
              cout << "The number of heads: " << heads << endl;
              cout << "The number of tails: " << tails << endl;

              return 0;
              }[/CODE]
              Last edited by Ganon11; Apr 12 '08, 02:59 AM. Reason: Please use the [CODE] tags provided.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Code:
                int random = 1 && 2;
                is not correct. That performs the logical expression 1 && 2, which should evaluate to 1 (or any nonzero integer) and assigns that value to random. So random's value is not random at all.

                Make your function, coin(), do something. In coin(), generate a random number - using rand() like you had before - and return it. Then, inside your loop, make an integer variable and call coin() to initialize it.

                Comment

                • haneeshkb
                  New Member
                  • Nov 2007
                  • 23

                  #9
                  Originally posted by PAK11
                  This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result should be return to the main program. I'm having trouble figuring it out, can anyone please help?


                  [code=cpp]
                  #include <iostream>
                  using namespace std;
                  # include <ctime>

                  int coin();

                  int main ()
                  {
                  int NUM_FLIPS = 100;
                  int count, face, heads = 0, tails = 0;

                  // initialize the random number generator
                  srand(static_ca st<int>(time(0) ));

                  // generate and count the number of heads and tails
                  for (int count=1; count <= NUM_FLIPS; count++)
                  {
                  face = coin();
                  if (face == 1)
                  heads++;
                  else
                  tails++;
                  cout << face << endl;
                  }

                  cout << "The number flips: " << NUM_FLIPS << endl;
                  cout << "The number of heads: " << heads << endl;
                  cout << "The number of tails: " << tails << endl;
                  }[/code]




                  use this code in your coin function

                  int coin()
                  {

                  int res;
                  randomize();
                  res= (rand() % 2);
                  return res; // will give either 0 or 1

                  }

                  Comment

                  Working...