bjstrat letters

c++ code to determine expected number of split hands

This algorithm determines the expected number of hands resulting from a pair split depending upon number of allowed splits. Initially we have a hand consisting of 2 pair cards to be split with a given number of allowed splits. The algorithm defines remaining splits (remSp) which = (allowed splits - 1). Other definitions are:
- undrawn to pair cards (pCards) which initially always = 2
- number of pair cards available to be drawn (p) = number of pairs cards available after 2 initial pair cards and up card have been dealt
- number of non-pair cards available to be drawn (np) = number of non-pairs cards available after 2 initial pair cards and up card have been dealt

This algorithm returns only the number of expected hands and is a precursor to this algorithm which parses expected hands into 2 types which can be used to compute expected values for splitting.

Code

double getTotHands(const int &p, const int &np, const int &pCards, const int &remSp)
{
	int tot;
	tot = p + np;
	
	/* eliminate drawing sequences that cannot be completed
	note: this only ensures that the basic framework of drawing
	generic pair cards (p) and non-pair cards (np) as the
	initial card is met; it does not guarantee that drawing
	requiremwnts after the initial draw can cause running
	out of cards; generally an undepleted shoe contains
	enough cards such that running out of cards is not
	a problem; if the basic framework cannot be met 0 hands
	is returned by this algorithm
	*/
	if (tot < 2 * remSp + pCards) {
		if (p >= remSp)
			return 0;
		else {
			if (np < pCards)
				return 0;
		}
	}

	if (p == 0 || remSp == 0)
		return (double)pCards;

	double pP, hands;

	pP = (double)p / tot;

	if (pCards >= 2) {
		hands = pP * getTotHands(p - 1, np, pCards + 1, remSp - 1);
		if (np > 0)
			hands += (1 - pP) * (getTotHands(p, np - 1, pCards - 1, remSp) + 1);
	}
	else // if (pCards == 1)
		hands = (1 - pP) + pP * getTotHands(p - 1, np, 2, remSp - 1);

	return hands;
}

Example: compute expected hands for splitting 2-2 v 3 dealt from 6 full decks, 3 allowed splits

int remSp = 2; // (allowed splits - 1) = (3 - 1)
// In 6 full decks there are 24 cards of rank 2, 288 cards of ranks other than 2
// After a pair of 2s and up card of 3 are dealt, 22 2s and 287 cards of rank other than 2 remain to be dealt:
int p = 22;
int np = 287;
int pCards = 2; // always the case initially
double hands = 0;

// run code
hands = getTotHands(p, np, pCards, remSp);
or alternatively using constants
hands = getTotHands(22, 287, 2, 2);

after code executes hands ~= 2.1599540968575321

 

Copyright 2010 (www.bjstrat.net)
All rights reserved.