I am trying to implement a generic algorithm for 2 in every 5 hit will return a true.2 is the allowed and 5 is the hits. In short this means if a function is called 5 times 2 of those times it will return true otherwise it will be a false. This is my try and it seems to be incorrect. Any suggestions on how I can fix it or if there is a better way to solve this ?
//2 in every 3 hits will return a true.
int hitlimit = 3;
int allowedlimit = 2;
int allowedTracker = 0;
int hitsTracker = 0;
bool test()
{
hitsTracker++;
if(allowedTracker < allowedlimit && hitsTracker < hitlimit)
{
allowedTracker++;
if(allowedTracker >= allowedlimit)
{
allowedTracker = 0 ;
hotstracker = 0;
}
return true;
}
return false;
}
I understand that you want to generate a deterministic sequence of allowedLimit
ones followed by hitlimit - allowedlimit
zeros. This is a periodic sequence that can be easily calculated from the current index:
int hitlimit = 3;
int allowedlimit = 2;
int hitsTracker = 0;
bool test() {
return hitsTracker++ % hitlimit < allowedlimit;
}