// filuppdelning krävs ej. // coin.h ---------------------------------------- #ifndef _COIN_H_ #define _COIN_H_ #include <string> class Coin { public: Coin(int nval) : nominal(nval) {} std::string toss() const; int getNominal() const { return nominal; } private: int nominal; }; #endif // coin.cc --------------------------------------- #include <cstdlib> std::string Coin::toss() const { return (rand() % 2 ? "head" : "tail"); } // main.cc --------------------------------------- using namespace std; #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(nullptr)); int nval; cout << "Mata in myntets nominella värde: "; cin >> nval; Coin c(nval); int winnings = 0; for (int i = 0; i < 1000000; ++i) { if (c.toss() == "head") winnings += c.getNominal(); else if (rand() % 100 < 52) winnings += c.getNominal(); else winnings -= c.getNominal(); } cout << "Total vinst blev " << winnings << " kr" << endl; return 0; } // Winnings in theory: 52% of total bets // 50% is win // 52% of remaining 50% is win // 0.5 + 0.5*0.52 = 0.76 // rest 0.24 is loose // 1000000*n*0.76 - 1000000*n*0.24 = 1000000*n*0.52