C++: Calculate probability percentage during each iteration
Posted
by
Mur Quirk
on Stack Overflow
See other posts from Stack Overflow
or by Mur Quirk
Published on 2012-11-28T04:58:26Z
Indexed on
2012/11/28
5:04 UTC
Read the original article
Hit count: 166
c++
Can't seem to get this to work. The idea is to calculate the percentage of heads and tails after each count, accumulating after each iteration. Except I keep getting nan% for my calculations. Anybody see what I'm doing wrong?
void flipCoin(time_t seconds, int flipCount){
vector<int> flips;
float headCount = 0;
float tailCount = 0;
double headProbability = double((headCount/(headCount + tailCount))*100);
double tailProbability = double((tailCount/(headCount + tailCount))*100);
for (int i=0; i < flipCount; i++) {
int flip = rand() % (HEADS - TAILS + 1) + TAILS;
flips.push_back(flip);
if (flips[i] == 1) {
tailCount++;
cout << "Tail Percent: " << tailProbability << "%" << endl;
}else{
headCount++;
cout << "Head Percent: " << headProbability << "%" << endl;
}
}
}
© Stack Overflow or respective owner