Skip to content

Instantly share code, notes, and snippets.

@odeblic
Created July 29, 2017 00:42
Show Gist options
  • Select an option

  • Save odeblic/2a40e325908f8faac67c860bdb3a0a31 to your computer and use it in GitHub Desktop.

Select an option

Save odeblic/2a40e325908f8faac67c860bdb3a0a31 to your computer and use it in GitHub Desktop.
Head or tail prediction
#include <iostream>
#include <ctime>
inline bool randomBool()
{
return rand() % 2;
}
int heads = 0;
int tails = 0;
int right = 0;
int wrong = 0;
int main()
{
srand(time(NULL));
while(heads + tails < 10000)
{
bool draw = randomBool();
if(heads != tails)
{
bool pre_draw = heads < tails;
if(draw == pre_draw)
{
right++;
}
else
{
wrong++;
}
}
//std::cout << "draw : " << (draw ? "heads" : "tails") << std::endl;
if(draw)
{
heads++;
}
else
{
tails++;
}
}
std::cout << "draws : " << heads + tails << std::endl;
std::cout << "heads : " << heads << std::endl;
std::cout << "tails : " << tails << std::endl;
std::cout << std::endl;
std::cout << "tries : " << right + wrong << std::endl;
std::cout << "right : " << right << std::endl;
std::cout << "wrong : " << wrong << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment