Game Development Reference
In-Depth Information
iGuess = iLow + ( ( iHigh - iLow ) / 2 );
// Check for correct guess
if ( InBucket( iGuess, Target ) ) {
return mvDistribution[iGuess].Result;
} // end if
// If not correct...
if ( Target > mvDistribution[iGuess].Edge ) {
// guess is too low, change the bottom boundary
iLow = iGuess;
} else {
// guess is too high, change the top boundary
iHigh = iGuess;
} // end if
} // end while
// Code should never get here!
assert( 0 && “Code fell through while loop!�);
return 0;
}
There is one main difference between this function and the binary search we
used earlier. Because the bucket is a range rather than a discrete point on the x -axis,
we must perform a slightly more involved check to see if our random number lands
in it. To do this, we create a function InBucket that takes our current bucket guess
and the random target as parameters.
bool CDistribution::InBucket( USHORT i, USHORT Target )
{
if ( i == 0 && Target <= mvDistribution[i].Edge ) {
return true;
} // end if
Search WWH ::




Custom Search