-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcoin_flip.cpp
More file actions
89 lines (77 loc) · 2.46 KB
/
coin_flip.cpp
File metadata and controls
89 lines (77 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
*
* Program: Coin Flip Simulator
*
* Description: Example of simulating a coin flip in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=ZSOgJmRQfdA
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <cstdlib>
#include <ctime>
// Use preprocessor constants to define HEADS and TAILS
#define HEADS 0
#define TAILS 1
using namespace std;
int flipCoin();
string flipCoin2();
int main()
{
// Seed the random number generator with the current time to ensure the
// possibility of different sequences of random numbers each time the program
// is executed.
srand((unsigned int) time(NULL));
// Test out the flipCoin() function. Output a sequence of 10 simulated coin
// flips.
for (int i = 0; i < 10; i++)
{
if (flipCoin() == HEADS) cout << "Heads" << endl;
else cout << "Tails";
}
// Counter variables for keeping track of the total number of heads and tails
// flipped in a sequence of flips.
int total_heads = 0;
int total_tails = 0;
// Test out the flipCoin2() function. Output the sequence of 10 simulated
// coin flips, while keeping track of the number of heads and tails flipped
// using the above counter variables.
for (int i = 0; i < 10; i++)
{
if (flipCoin2() == "Heads")
{
total_heads++;
cout << "Heads" << endl;
}
else
{
total_tails++;
cout << "Tails" << endl;
}
}
// Output the total number of heads and tails flipped
cout << "Total Heads: " << total_heads << endl;
cout << "Total Tails: " << total_tails << endl;
return 0;
}
// Returns HEADS (0) or TAILS (1) randomly. The rand() function returns an
// integer between 0 and a large positive integer. The modulus operator %
// returns the remainder of a division operation. If we take any integer
// between 0 and a large positive integer and divide it by 2 the only
// possible remainders are 0 and 1. In this way, we can randomly generate
// either 0 for HEADS or 1 for TAILS.
int flipCoin()
{
if (rand() % 2 == 0) return HEADS;
else return TAILS;
}
// Returns the string "Heads" or "Tails" instead of an int representation, the
// logic for using rand() to randomly generate one of the two possibilities
// is the same.
string flipCoin2()
{
if (rand() % 2 == 0) return "Heads";
else return "Tails";
}