Sunday, October 19, 2025

Average Distance from Center of a Unit Cube to Its Surface

Let’s raise the stakes by a dimension. Now, you start at the center of a unit cube. Again, you pick a random direction to move in, with all directions being equally likely. You move along this direction until you reach a point on the surface of the unit cube. On average, how far can you expect to have traveled?

So unlike shifting the shape that we are trying to hit the perimeter of, in the Extra Credit problem, we instead increase the dimensionality. Here $$B_\infty = \left\{ (x,y,z) \in \mathbb{R}^3 \mid \max \{ |x|, |y|, |z| \} = \frac{1}{2} \right\}.$$ Also, instead of parameterizing the direction of travel by a single bearing with respect to the positive $x$-axis, here we need to have two angles, one $\theta \in [0, \pi]$ with respect to the positive $z$-axis and a second $\varphi \in [0,2\pi)$ with respect to the positive $x$-axis. Since the differential of the solid angle of a region of the surface of the sphere is $d\Omega = \sin \theta \,d\varphi \,d\theta$ and the entire surface area of a unit sphere is $4\pi,$ we see that the join distribution function for $\theta$ and $\varphi$ is $$f(\theta, \varphi) = \frac{\sin \theta}{4\pi}.$$ Now, again assuming a unit speed, and given we get the position vector $$r(t) = ( t \cos \varphi \sin \theta, t \sin \varphi \sin \theta, t \cos \theta ).$$

Since you will hit $B_\infty$ whenever $t \max \{ |\cos \varphi \sin \theta|, |\sin \varphi \sin \theta|, |\cos \theta| \} = \frac{1}{2},$ so therefore the distance traveled is \begin{align*} d(\theta, \varphi) &= \frac{1}{2 \max \{ |\cos \varphi \sin \theta|, |\sin \varphi \sin \theta|, |\cos \theta| \}} \\ & = \frac{1}{2} \min \{ |\sec \varphi| \csc \theta, |\csc \varphi| \csc \theta, |\sec \theta | \}, \end{align*} where the final equation removes $\csc \theta$ from the absolute values since $\sin \theta, \csc \theta \geq 0$ for all $\theta \in [0,\pi]$

Therefore, the average distance is $$\hat{d} = \frac{1}{4\pi} \int_0^\pi \int_0^{2\pi} d(\theta, \varphi) \sin \theta \,d\varphi \, d\theta.$$ Now we can decompose this integral into three regions, two of which seem to have analytical solutions, so let's try even though we could just stop here and do the larger integral right here and right now.

Let's only worry about the upper hemisphere, that is $0 \leq \theta \leq \frac{\pi}{2}$. Here we can then break the range into the following three sections:

  • Region I - when $0 \leq \theta \leq \frac{\pi}{4},$ where you will eventually hit the top of the unit cube no matter what the value of $\varphi$;
  • Region II - when $\cos^{-1} \frac{1}{\sqrt{3}} \leq \theta \leq \frac{\pi}{2},$ where you will eventually hit one of the vertical sides of the unit cube no matter what the value of $\varphi$; and
  • Region III - when $\frac{\pi}{4} \leq \theta \leq \cos^{-1} \frac{1}{\sqrt{3}},$ where depending on the value of $\varphi$ you will either hit the top or the sides.

In other words, in Region I, $$d_1(\theta, \varphi) = \frac{1}{2} |\sec \theta|, \forall \varphi \in [0,2\pi),$$ so we have \begin{align*}I_1 &= \frac{1}{2\pi} \int_0^{\pi/4} \int_0^{2\pi} d_1(\theta, \varphi) \,d\varphi \, \sin \theta \,d\theta \\ &= \frac{1}{2\pi} \int_0^{\pi/4} \pi \tan \theta \, d\theta = \left.-\frac{1}{2} \ln | \cos \theta | \right|^{\pi/4}_0 \\ &= \frac{1}{4} \ln 2 \approx 0.17328679514\dots.\end{align*} In Region II, $$d_2(\theta, \varphi) = \frac{\csc \theta}{2} \min \{ |\sec \varphi|, |\csc \varphi| \},$$ so we have \begin{align*} I_2 &= \frac{1}{2\pi} \int_{\cos^{-1} (1/\sqrt{3})}^{\pi/2} \int_0^{2\pi} \frac{1}{2} \min \{ |\sec \varphi|, |\csc \varphi| \} \,d\varphi \csc \theta \sin \theta \,d\theta \\ &= \int_{\cos^{-1} (1/\sqrt{3})}^{\pi/2} \left( \frac{1}{2\pi} \int_0^{2\pi} \frac{1}{2} \min \{ |\sec \varphi|, |\csc \varphi| \} \,d\varphi \right) \,d\theta\\ & = \frac{2}{\pi} \ln ( 1 + \sqrt{2} ) \left( \frac{\pi}{2} - \cos^{-1} \left(\frac{1}{\sqrt{3}}\right) \right)\\ & = \left( 1 - \frac{ 2 \cos^{-1} \left( \frac{1}{\sqrt{3}} \right) }{\pi} \right) \ln (1 + \sqrt{2}) \approx 0.345345573653\dots,\end{align*} since the integral with respect to $\varphi$ is none other than the expected distance traveled inside a square, which we found the solution for in the Classic Problem. The trickier third region has integral $$I_3 = \int_{\pi/4}^{\cos^{-1} (1/\sqrt{3})} \int_0^{2\pi} d(\theta,\varphi) \,d\varphi \sin \theta \, d\theta \approx 0.0920550322989\dots,$$ which does not seem to have a readily available analytical solution. Putting these regions together we get that the overall average distance traveled to the surface of the unit cube when uniformly randomly choosing a direction is $$\hat{d} = I_1 + I_2 + I_3 = \frac{1}{4} \ln 2 + \left( 1 - \frac{ 2 \cos^{-1} \left( \frac{1}{\sqrt{3}} \right) }{\pi} \right) \ln (1 + \sqrt{2}) + I_3 \approx 0.610687401568\dots.$$

Average Distance from Center of a Unit Square to Its Perimeter

You start at the center of the unit square and then pick a random direction to move in, with all directions being equally likely. You move along this chosen direction until you reach a point on the perimeter of the unit square. On average, how far can you expect to have traveled?

Let's assume that you start at the origin, which is the center of the unit square $B_\infty = \{ (x,y) \in \mathbb{R}^2 \mid \max \{ |x|, |y| \} = \frac{1}{2} \}.$ Further, let's assume that your randomly chosen bearing makes an angle of $\theta$ with the positive $x$-axis, so $\theta \sim U(0,2\pi),$ and let's assume that you move at some unit speed, so your position through time will be $r(t) = (t \cos \theta, t \sin \theta).$

Since you will hit the $B_\infty$ exactly when $t \max \{ |\cos \theta|, |\sin \theta| \} = \frac{1}{2}$, therefore, the distance you would travel from the origin is given by $$d(\theta) = \frac{1}{2 \max \{ |\cos \theta|, |\sin \theta| \} } = \frac{1}{2} \min \{ |\sec \theta|, |\csc \theta| \}.$$ Relying on the periodicity of $d(\theta)$ to break the entire integral into 8 equal parts, we get thatthe expected distance is therefore \begin{align*}\hat{d} &= \frac{1}{2\pi} \int_0^{2\pi} d(\theta) \,d\theta \\ &= \frac{1}{2\pi} \left( 8 \int_0^{\pi/4} \frac{1}{2} \sec \theta \,d\theta \right) \\ &= \frac{2}{\pi} \Biggl.\ln \left| \sec \theta + \tan \theta \right| \Biggr|^{\pi/4}_0 \\ &= \frac{2}{\pi} \ln \left( 1 + \sqrt{2} \right) \approx 0.561099852339\dots.\end{align*}

We can extend this problem to the slightly different problem of starting at the origin, pointing a random direction and traveling at a uniform, unit Euclidean speed until we get to any the perimeter of any half-unit ball, say $B_p = \{ (x,y) \in \mathbb{R}^2 \mid |x|^p + |y|^p = \frac{1}{2^p} \}.$ As you could possibly surmise, the official question was $B_\infty,$ which is the limit as $p \to \infty.$ In this case, we would get $$d_p (\theta) = \frac{1}{2 \sqrt[p]{\cos^p \theta + \sin^p \theta}}$$ and $$\bar{d}_p = \frac{2}{\pi} \int_0^{\pi/4} \frac{d\theta}{\sqrt[p]{\cos^p \theta + \sin^p \theta}} = \frac{4}{\pi} \int_0^{\sqrt{2}-1} \frac{dt}{\sqrt[p]{(1-t^2)^p + 2^p t^p}},$$ where the second integral is obtains through the mystical tangent half-angle substitution. In general, this is not an analytical answer, though in certain other cases it simplifies. Obviously, for the case of $p=2$ everything simplifies to $d_2(\theta) = \frac{1}{2}, \forall \theta \in [0,2\pi)$ and so $\bar{d}_2 = \frac{1}{2}$ as well. For $p=1$, we have $$d_1(p) = \frac{1}{2 (\cos \theta + \sin \theta)}$$ and \begin{align*}\bar{d}_1 &= \frac{4}{\pi} \int_0^{\sqrt{2}-1} \frac{dt}{(1-t^2) + 2t}\\ &= \frac{4}{\pi} \int_0^{\sqrt{2}-1} \frac{dt}{1+2t-t^2}\\ &= \frac{\sqrt{2}}{\pi} \int_0^{\sqrt{2}-1} \frac{1}{1 + \sqrt{2} - t} + \frac{1}{\sqrt{2} - 1 + t} \,dt \\ &= \frac{\sqrt{2}}{\pi} \left. \ln \left| \frac{\sqrt{2} - 1 + t}{\sqrt{2} + 1 - t} \right| \right|_0^{\sqrt{2}-1}\\ & = \frac{\sqrt{2}}{\pi} \left( \ln \left(\frac{2\sqrt{2}-2}{2} \right) - \ln \left( \frac{\sqrt{2}-1}{\sqrt{2}+1} \right) \right)\\ &= \frac{\sqrt{2}}{\pi} \ln ( 1 + \sqrt{2} ) \approx 0.396757510512\dots.\end{align*} For completeness's sake, below is a table for various other values of $p.$

$p$ $\bar{d}_p$
$1$ $0.396757510512\dots$
$1.5$ $0.466430018867\dots$
$2$ $0.500000000000\dots$
$2.5$ $0.518545527769\dots$
$3$ $0.529817160992\dots$
$4$ $0.542203450927\dots$
$5$ $0.548481398011\dots$
$10$ $0.557672594762\dots$
$100$ $0.561063099606\dots$
$1000$ $0.56109948237\dots$
$\infty$ $0.561099852339\dots$

Monday, October 13, 2025

Let's Make a Tic-Tac-Deal, Extra Credit

In the actual game, you get five rolls instead of three. But as with rolling a 2 or 12, rolling a number that you have already rolled is a wasted turn. With five rolls of the dice, what are your chances of getting three Xs in a row, either horizontally, vertically, or diagonally?

Let's just simulate our way out of this. We have the same 8 possible winning sets, that we will encode as sets in Python and then after simulating a 5 rolls of two dice, we can check whether the resulting set of roll outcomes contains any of the winning sets. See the code snippet below that we can use to encode playing a random round of Tic-Tac-Deal.

Running $1$ million simulations, we infer that the probability of winning the game with $5$ dice rolls is about $36.0907\dots\%.$ In particular, the $95\%$ confidence interval is $(35.996568\%, 36.184832\%).$ Further, note that if we run these simulations with the parameter $rolls = 3,$ then we get a $95\%$ confidence interval of $(5.725294\%, 5.816706\%)$ which certainly contains our analytics answer of $\frac{113}{1944}.$

Let's Make a Tic-Tac-Deal!

The game of Tic-Tac-Deal $2.0$ has a $3$-by-$3$ square grid with the numbers $3$ through $11$, arranged as follows:

$3$ $4$ $5$
$6$ $7$ $8$
$9$ $10$ $11$

You start by rolling a standard pair of six-sided dice and add the two numbers rolled. You place an X on the board on the square that contains the sum. If the sum is a $2$ or $12$, your roll is wasted. If you have exactly three rolls of the dice, what are your chances of getting three Xs in a row (either horizontally, vertically, or diagonally)?

First, we see that $$p_n = \frac{\min\{n-1,13-n\}}{36}$$ is the probability of getting a vakue of $n$ on a roll of two standard dice. Secondly, we see that there are a total of 8 winning configurations: $\{3,4,5\}$, $\{3,6,9\}$, $\{3,7,11\}$, $\{4,7,10\}$, $\{5,7,9\}$, $\{5,8,11\}$, $\{6,7,8\}$, and $\{9,10,11\}.$

Since order doesn't matter here, the probability of getting a winning configuration $W=\{w_1, w_2, w_3\}$ is given by $$p(W)= 3! \prod_{i=1}^3 p_{w_i}.$$ So the overall probability of winning in three rolls is \begin{align*}P=\sum_W p(W)&= 3! \left( p_3p_4p_5 + p_3p_6p_9 + p_3p_7p_{11}\right. \\ &\quad\quad +p_4p_7p_{10} + p_5p_8p_{11} + p_5p_7p_9 \\ &\left.\quad\quad\quad + p_6p_7p_8 + p_9p_{10}p_{11} \right)\\ &= \frac{\left( 144 + 240 + 144 + 324 + 576 + 240 + 900 + 144 \right)}{46656} \\ &= \frac{113}{1944} \approx 5.812757\dots\%\end{align*}

Monday, October 6, 2025

What is Anita's 𝝋?

It’s time for you to check Anita’s work. What was the measure of angle $\varphi$?

Ok, so as we saw in the prior Classic Fiddler problem, Anita will be crossing the $1$-inch segment while she is on her $4$-inch line segment, that is when she is traveling from $$X_3 = (1+2\cos \varphi+3\cos 2\varphi, 2\sin \varphi + 3\sin 2\varphi)$$ to $$X_4 = (1 + 2 \cos \varphi + 3 \cos 2\varphi + 4\cos 3\varphi, 2 \sin \varphi + 3 \sin 2\varphi + 4 \sin 3 \varphi).$$ We have already somewhat loosely located $\varphi \in ( \frac{3\pi}{4}, \frac{4\pi}{5} ).$

We see that the line from $X_3$ to $X_4$ is given by $$y - 2 \sin \varphi - 3 \sin 2\varphi = \tan 3\varphi \left( x - 1 - 2 \cos \varphi - 3 \cos 2\varphi \right).$$ Additionally we note that since for large angles (e.g., $\phi = \pi$) the crossing point is near $(0,0)$ and since the crossing point for the smaller $\phi = \frac{4\pi}{5}$ being closer towards the $(1,0)$ endpoint of the initial segment, it stands to reason that for the optimal angle $\varphi,$ that the crossing point would be precisely at the endpoint $(1,0).$ In this cas we would have $$ - 2 \sin \varphi - 3 \sin 2 \varphi = \tan 3\varphi \left( -2 \cos \varphi - 3 \cos 2\varphi \right).$$ Since $$\tan 3 \varphi = \frac{ 3 \tan \varphi - \tan^3 \varphi }{ 1 - 3 \tan^2 \varphi } = \tan \varphi \frac{ 3 - \tan^2 \varphi }{ 1 - 3 \tan^2 \varphi } = \tan \varphi \frac{ 4 \cos^2 \varphi - 1}{ 4 \tan^2 \varphi - 3},$$ $$-2 \cos \varphi - 3 \cos 2\varphi = 3 - 2 \cos \varphi - 6 \cos^2 \varphi,$$ and $$2 \sin\varphi + 3 \sin 2\varphi = 2 \tan \varphi \cos \varphi ( 1 + 3 \cos \varphi ),$$ the above equality is equivalent (which much algebraic expansion and reduction) to \begin{align*} 0 &= \tan \varphi \frac{4 \cos^2 \varphi - 1}{4 \cos^2 \varphi - 3} ( 3 - 2\cos \varphi - 6 \cos^2 \varphi) + 2 \tan \varphi \cos \varphi (1 + 3 \cos \varphi) \\ &= \frac{\tan \varphi}{4 \cos^3 \varphi - 3} \left( (4 \cos^2 \varphi - 1) ( 3 - 2 \cos\varphi - 6 \cos^2 \varphi ) + 2 \cos \varphi ( 4 \cos^2 \varphi - 3 ) (1 + 3 \cos \varphi) \right) \\ &= - \frac{\tan \varphi (4 \cos \varphi + 3)}{4 \cos^2 \varphi - 3},\end{align*} so we have that $\varphi = \cos^{-1} \left(-\frac{3}{4}\right) \approx 2.41885840578\dots.$

Anita's Walk

Anita the ant is going for a walk in the sand, leaving a trail as she goes. First, she walks $1$ inch in a straight line. Then she rotates counterclockwise by an angle $\varphi$, after which she walks another $2$ inches. She rotates counterclockwise an angle $\varphi$ again, after which she walks $3$ inches. She keeps doing this over and over again, rotating counterclockwise an angle $\varphi$ and then walking $1$ inch farther than she did in the previous segment.

At some point during her journey, she crosses over her initial $1$-inch segment. By “cross over,” I am including the two end points of that first segment. Anita realizes that $\varphi$ was the smallest possible angle such that she crossed over her $1$-inch segment. (Among the ants, she’s known for her mathematical prowess.) How long was the segment along which she first crossed over the $1$-inch segment? Your answer should be a whole number of inches.

Let's let Anita's position on the $xy$-plane with origin centered at her trail's beginning after completing her line segment of length $n$, be $X_n = (x_n, y_n)$. In particular we then see that $$x_n = \sum_{k=1}^n k \cos ((k-1) \varphi) \,\,\text{and}\,\, y_n = \sum_{k=1}^n k \sin ((k-1) \varphi).$$ One thing we notice immediately is that as $n \to \infty,$ $X_n$ does not converge and in fact $\|X_n\| \to \infty.$ Therefore, if there is crossing over the initial segment it would have to happen in the first handful of steps, rather than after some extremely large cycle.

We see that if $\phi=\pi,$ then obviously Anita would de facto double back into her first line segment in her $n=2$ step, since she would start retracing her steps immediately. Let's try to see whether or not there can be some $\phi \lt \pi$ such that Anita crosses the first line segment in her $n=3$ step. In this case we see that $X_2 = (1 + 2\cos \phi, 2\sin \phi)$ and $X_3 = (1 + 2 \cos \phi + 3 \cos 2\phi, 2\sin \phi + 3\sin 2\phi),$ so the line connecting $X_2$ and $X_3$ is given by $$y - 2\sin \phi = \tan 2\phi \left( x - 1 - 2 \cos \phi \right).$$ In particular, if we assume that Anita crossed at some point $(h, 0)$ on the initial line segment, that is with $0 \leq h \leq 1,$ then we would get the equality \begin{align*}0 &= 2 \sin \phi \left( 1 - \frac{ \cos \phi \left( 2 \cos \phi + 1 - h \right) }{ \cos 2\phi } \right)\\ &= \frac{2 \sin \phi}{\cos 2\phi} \left( \cos 2\phi - \cos \phi \left( 2\cos \phi + 1 - h \right) \right) \\ &= \frac{2 \sin \phi}{\cos 2\phi} \left( 2 \cos^2 \phi - 1 - \left(2 \cos^2 \phi + (1-h) \cos \phi\right) \right) \\ &= -\frac{2\sin \phi}{\cos 2\phi} \left( 1 + (1-h) \cos \phi \right)\end{align*} For $h \in (0,1],$ we have $-\frac{1}{1-h} \lt -1,$ so there are no solutions of $\phi \in \mathbb{R}$ with $1 + (1-h) \cos \phi = 0.$ Therefore, besides the case of $\phi = \pi$ as we saw before, there are no solutions where Anita crosses the $1$-inch segment on her $n=3$ step.

Let's look at the case of say $\phi = \frac{4\pi}{5}.$ In this case, we see that $X_3 = (0.309017\dots, -1.677599\dots)$ and $X_4=(1.545085\dots, 2.126627\dots),$ so the $n=4$ line segment crosses the $1$-inch line segment at $(h,0)$ for $h\approx 0.854102\dots.$ So there is some value of $\phi$ for which Anita would cross the $1$-inch segment on the $n=4$ segment.

Let's look at another case where $\phi=\frac{3\pi}{4}.$ Here we see the $X_3=(1-\sqrt{2}, \sqrt{2}-3)$ and $X_4=(1+\sqrt{2},3\sqrt{2}-3).$ The line segment between $X_3$ and $X_4$ narrowly missed the $1$-inch segment, crossing at $(4-2\sqrt{2},0)$. We can show (or at least vigorously wave our hands) that there are no angle $\phi \leq \frac{3\pi}{4}$ that there are similarly no instances where Anita would cross the $1$-inch segment.

Therefore, we see that the length of the segment that Anita crossed the initial path with was four.

Monday, September 29, 2025

Riskier Risk, Now with Wilds!

The full deck of Risk cards also contains two wildcards, which can be used as any of the three types of cards (infantry, cavalry, and artillery) upon trading them in. Thus, the full deck consists of $44$ cards.

You must have at least three cards to have any shot at trading them in. Meanwhile, having five cards guarantees that you have three you can trade in.

If you are randomly dealt cards from a complete deck of $44$ one at a time, how many cards would you need, on average, until you can trade in three? (Your answer should be somewhere between three and five. And no, it’s not four.)

Let $S$ be the random number of cards you have when you can first turn in a set of three cards. Obviously we have $3\leq S\leq 5,$ so lets go about calculating $p_s = \mathbb{P} \{ S= s\},$ for $s \in \{3, 4, 5\},$ which we can then compute $$\mathbb{E} [S] = \sum_{s=3}^5 sp_s.$$

From the Classic wildless Risk problem, we see that there remain $3836$ possible combinations of three card sets without using any wilds. To this we must at $\binom{2}{1} \binom{42}{2} = 1722$ possible combinations of three card sets with ome wild card and $\binom{2}{2}\binom{42}{1}=42$ possible combinations of three card sets with two wild cards. In total, there are $5600$ possible sets of three cards, out of a total $\binom{44}{3}=13244$ possible hands of three Risk cards, so we have $$p_3 = \frac{5600}{13244}=\frac{200}{473}\approx 42.283\dots\%$$

Rather than the messiness of figuring out the exact combinations for $S=4$, let's instead tackle $S=5.$ We see that if you do not get a set on your fourth card then you will automatically get a set by your fifth card, so we only need to figure out what situations lead you to not have a set after four cards. This can only happen whenever you have two of one type and two of a different type (with no wilds, obviously). There are $\binom{14}{2}\binom{14}{2}\binom{3}{2}=24843$ possible hands that have exactly two cards in two different types. Since there are $\binom{44}{4} = 135751$ possible combinations of four distinct Risk cards, we thus have $$p_5=\frac{24843}{135751}=\frac{3549}{19393}\approx 18.300\dots\%$$

Since we can reason that $p_4=1-p_3-p_5 = \frac{7644}{19393}\approx 39.416\dots\%,$ we have the expected number of Risk cards needed to turn in a set is $$\mathbb{E}[S]= 3 \frac{200}{473} + 4 \frac{7644}{19393} + 5 \frac{3549}{19394} = \frac{72921}{19393} \approx 3.760171\dots$$