// Prog: choosing_numbers.cpp
// let your loaded dice play against your friend's dice
// in the game of choosing numbers.
#include
#include
// POST: return value is the payoff to you (possibly negative),
// given the numbers of you and your friend
int your_payoff (const unsigned int you, const unsigned int your_friend)
{
if (you == your_friend) return 0; // draw
if (you < your_friend) {
if (you + 1 == your_friend) return 2; // you win 2
return -1; // you lose 1
} // now we have your_friend < you
if (your_friend + 1 == you) return -2; // you lose 2
return 1; // you win 1
}
int main() {
// the random number generator; let us use the generator
// ANSIC instead of the toy generator knuth8; m = 2^31;
ifm::random ansic (1103515245u, 12345u, 2147483648u, 12345u);
// your strategy may be to prefer larger numbers and use
// the distribution (1/21, 2/21, 3/21, 4/21, 5/21, 6/21)
const double p = 1.0/21.0;
ifm::loaded_dice you (p, 2*p, 3*p, 4*p, 5*p, ansic);
// your friend's strategy may be to stay more in the middle
// and use the distribution (1/12, 2/12, 3/12, 3/12, 2/12, 1/12)
const double q = 1.0/12.0;
ifm::loaded_dice your_friend (q, 2*q, 3*q, 3*q, 2*q, ansic);
// now simulate 1 million rounds (the train may be very late...)
int your_total_payoff = 0;
for (unsigned int round = 0; round < 1000000; round++) {
your_total_payoff += your_payoff (you(), your_friend());
}
// output the result:
std::cout << "Your total payoff is "
<< your_total_payoff << "\n";
return 0;
}