Coders Packet

Coin Toss Program using C++

By G Ganga Jathin

Building a Coin Toss program using the rand() function of the pseudo-random algorithm in C++

This packet deals with the Coin Toss Prediction program which uses the famous rand() function in C++
which uses a pseudo-random algorithm to produce random heads and tails of a coin when it is flipped.

The main idea of this code is,

    int randomtoss = Toss();
    if (randomtoss == 1)
      result= "head";
      result = "tail";
    cout <<result<< endl;
    cout << "Do you want to continue (enter y/n)" << endl;
    cin >> ch;

  }while(ch == 'y');
    cout << "Thank you for using this program !!!!" << endl;

As we can see the do while continuously re iterates to generate heads or tails until the user desires

int Toss() {
  int randomNumber;
  randomNumber = rand() % 2;
  return randomNumber;

The Toss function here generates random order of 0's and 1's by making it check with parity of random number generated by rand() function



Download the zip file 
extract it and open the source code in your desired ide
and run the program.

Download Complete Code


No comments yet