# include # include using namespace std; int main ( ) // // We are going to win $1,000 from Bill Gates by betting on a coin toss. // // If we don't win the first toss, we try again...and again. // // To simulate a coin toss, we call "rand()", which returns a random // integer. If the integer is odd, we win the toss. // In order to get a different random number sequence each time we play, // we have to call the "srand ( )" function. { int bet; int coin; int tries; int won; srand ( time ( NULL ) ); won = 0; bet = 1000; tries = 1; coin = rand ( ); while ( coin % 2 == 0 ) { won = won - bet; bet = 2 * bet; tries = tries + 1; coin = rand ( ); } won = won + bet; cout << "\n"; cout << "Success!\n"; cout << "After " << tries << " coin flips, Bill Gates paid us $" << bet << "\n"; cout << "Our total winnings are " << won << "\n"; return 0; }