Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int coinFlip()
- {
- static unsigned int calls = 0, total = 0; //calls: number of times function has been called; total: accumulative total of flips
- unsigned int sum = 0, flips = 0; //sum: sum of H/T(H = 0, T = 1)
- do { //flip the coin 9 times...
- sum += (rand() % 2); //H = 0, T = 1
- flips++;
- } while (flips < 9);
- //total += flips;
- if (sum == 0) //sum of 9 heads is 0, since H = 0
- {
- /*cout << "Total flips: " << total
- << "\nNumber of trials needed to get 9 heads: " << calls << endl;*/
- return flips;
- }
- else
- {
- calls++;
- coinFlip(); //call the function again...
- }
- }
- int main()
- {
- srand(time(NULL)); //seed rand(), so we get a "random" number from it...
- //multithread();
- unsigned int total = 0;
- for (int i = 0; i < 100; i++)
- total += coinFlip();
- cout << "Number of trials needed: " << round(total / 100.0) << endl;
- cout << "Press enter to exit...";
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement