Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void 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 to needed get 9 heads: " << calls << endl;
- }
- else
- {
- calls++;
- coinFlip(); //call the function again...
- }
- }
- int main()
- {
- srand(time(NULL)); //seed rand(), so we get a "random" number from it...
- //multithread();
- coinFlip();
- cout << "Press enter to exit...";
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement