Sunday, May 22, 2022

Coin Flips Simulation in C++

 A simple coin flips simulation in C++ programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Please subscribe to my channel  https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg

=================================================


Want to support my channel?

GCash Account

Jake Pomperada

09173084360


Paypal

https://paypal.me/jakerpomperada


Patreon

https://www.patreon.com/jakerpomperada


Thank you very much for your support.





Program Listing

#include <iostream>
#include <random>
#include <chrono>

using namespace std;

int main()
{
   std::default_random_engine generator;
   std::uniform_int_distribution<int> distribution(0,1);
   generator.seed(time(0));
   int flips{};
   cout <<"\n\n";
   cout << "\tCoin Flips Simulation in C++";
   cout <<"\n\n";
    cout << "\tHow many flips : ";
   cin >> flips;
   //you should not time the cin statement, as that introduces user
//response time in result.
  cout <<"\n\n";
   auto start = chrono::steady_clock::now();
   int heads{};
   for(int i{}; i<flips; i++)
       {
         heads += distribution(generator);
       }
    cout << "\theads: " << heads << " tails: " << flips-heads << endl;
    auto end = chrono::steady_clock::now();
    cout <<"\n\n";
    cout << "\tElapsed time: "
         << chrono::duration_cast<chrono::milliseconds>(end - start).count()
         << "ms\n";
   cout <<"\n\n";
   cout << "\tEnd of Program";
   cout <<"\n\n";
}


No comments:

Post a Comment