当前位置: 首页>>代码示例>>C++>>正文


C++ Coin::toss方法代码示例

本文整理汇总了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;
}
开发者ID:jmcilhargey,项目名称:comp-sci-assignments,代码行数:14,代码来源:main.cpp

示例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;
}
开发者ID:GearAntLaboratories,项目名称:Console_Game_Suite,代码行数:48,代码来源:coin.cpp


注:本文中的Coin::toss方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。