Books · The Fiddler: Solutions
Chapter 53
How Long Is the River?
In the language Fiddlish every word is three or four letters long (each equally likely), and words are separated by single spaces. Deep into a long line of text, what is the probability that a given character is a space?
The Fiddler, Zach Wissner-Gross, May 23, 2025(original post)
Solution
Each word contributes, on average, letters plus its one trailing space: characters per word, exactly one of them a space. By the renewal theorem the long-run fraction of spaces is
The computation
Generate the actual stream: draw millions of words of three or four letters, append one space to each, and take the fraction of all characters that are spaces.
import numpy as np
rng = np.random.default_rng(0)
words = rng.integers(3, 5, 5_000_000) # 3 or 4 letters, equally likely
spaces = len(words); total = int((words + 1).sum()) # +1 space per word
print(round(spaces / total, 4)) # 0.2222 = 2/9
Extra Credit
In a monospace font, suppose the th character of a line is a space. A “river” runs diagonally down and to the right: line continues it if its th character is a space, line if its th is a space, and so on. How long is the river on average?
Solution
Different lines are independent, so the river continues to each next line with the probability that that line has a space at the relevant position. Crucially those positions () are near the start of the line, where the space probability has not yet settled to . Writing for the renewal probability of a space at position (steps of or , each ), (The source’s value is paywalled; this is my own, with well above .)
The computation
Build the renewal probability that position is a space (each space sits or characters past the previous, equally likely), then accumulate the river’s expected length as the running product of continuation probabilities from position on.
from fractions import Fraction as F
u = [F(0)] * 160; u[0] = F(1)
for q in range(1, 160):
u[q] = (u[q - 4] if q >= 4 else 0) / 2 + (u[q - 5] if q >= 5 else 0) / 2
E = F(1); prod = F(1)
for j in range(13, 160): prod *= u[j]; E += prod
print(float(E)) # 1.5347