本文整理汇总了C++中Coin::toss方法的典型用法代码示例。如果您正苦于以下问题:C++ Coin::toss方法的具体用法?C++ Coin::toss怎么用?C++ Coin::toss使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Coin
的用法示例。
在下文中一共展示了Coin::toss方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
Coin coin;
const int TURNS = 20;
cout << "The initial side is: " << coin.getSideUp() << endl;
for (int i = 0; i < TURNS; i++) {
coin.toss();
cout << "For turn #" << i + 1 << " the side is: " << coin.getSideUp() << endl;
}
return 0;
}
示例2: CoinFlip
int CoinFlip() {
Coin coin;
int numTosses;
int expectedHeads;
int expectedTails;
int numSimulations = 100;
int numHeads = 0;
int numTails = 0;
int count = 0;
cout << "Enter the number of tosses for each simulation:" << endl;
cin >> numTosses;
cout << "Enter Expected Return of Heads. Number must be less than " << numTosses << endl;
cin >> expectedHeads;
expectedTails = numTosses - expectedHeads;
cout << "Monte Carlo Simulation" << endl;
cout << "----------------------" << endl;
cout << "Number of coin tosses: " << numTosses << endl;
cout << "Expected Number of Heads: " << expectedHeads << endl;
cout << "Expected Number of Tails: " << expectedTails << endl;
cout << "Number of simulations to be run: " << numSimulations << endl;
cout << "Press any key to start runnig simulations:" << endl;
system("pause");
for (int i = 0; i<numSimulations; i++)
{
numHeads = 0;
numTails = 0;
for (int j = 0; j<numTosses; j++) {
if (coin.toss().compare("Heads")) numHeads++; else numTails++;
}
cout << "Heads: " << numHeads << " Tails: " << numTails << endl;
if (numHeads>expectedHeads) count++;
}
cout << "-------------------" << endl;
cout << "Simulation Complete" << endl;
cout << "-------------------" << endl;
cout << "Total number of simulations that resulted in Head values greater than the expected result of " << expectedHeads << " is " << count << endl;
cout << "Now compare the simulated probability to the binomial distribution calculated probability" << endl;
cout << "Simulated probability: " << ((double)count / numSimulations) << endl;
system("pause");
return 0;
}