Skip to content
Vamshi Jandhyala

Books · The Fiddler: Solutions

Chapter 1

Can You Fix the Random Number Generator?

↓ Download PDF handout

A calculator’s random number key should return a value spread evenly between 00 and 11, with every value equally likely. This one is broken: it returns a value spread evenly between 00 and some fixed aa, where aa is an unknown number between 00 and 11. Before you switch it on you have no idea what aa is, so you treat it as equally likely to be anywhere in that range. You press the key once and read off exactly 0.50.5. Given that single reading, what is the expected value of aa?

For extra credit: a second broken calculator works the same way, with its own unknown aa spread evenly over 00 to 11. This time you do not see the reading itself, only that it landed somewhere between 00 and 0.50.5. Now what is the expected value of aa?

The Fiddler, Zach Wissner-Gross, June 19, 2026(original post)

Solution

Everything turns on the ceiling aa, the value the broken calculator never climbs above. Before the key is pressed aa could sit anywhere in (0,1)(0,1) with equal claim, a flat prior f(a)=1f(a) = 1, and the single reading is the only thing that sharpens that belief. The expected value we are after is the average of the sharpened belief, so the task is to see how the reading reshapes it.

Start from what a calculator of a known ceiling does. One with ceiling aa scatters its output evenly across (0,a)(0,a), so it returns a value near xx with density 1/a1/a, as long as xx does not exceed aa, for a machine can never report a number above its own ceiling. Now read this the other way, as a statement about the ceiling for the value we actually saw, and two things stand out. A reading of 0.50.5 throws out every ceiling below 0.50.5 outright, since those machines could not have produced it; and among the ceilings that survive, the lower ones are the better bet, because a calculator with a narrow range packs its output into a short interval and is the likelier source of any particular reading. That second effect is precisely the factor 1/a1/a.

Those two observations are Bayes’ rule for this problem. The sharpened belief in aa, its posterior density, is the prior cut down to the survivors and tilted by 1/a1/a, f(ax)1a,xa1,f(a \mid x) \propto \frac1a, \qquad x \le a \le 1, and dividing by its total x1daa=lnx\int_x^1 \frac{da}{a} = -\ln x turns the proportionality into a genuine density. The average ceiling is this density weighted by aa and summed across the survivors, where the weight aa neatly cancels the 1/a1/a and leaves a flat integrand, E[aX=x]=x1af(ax)da=1lnxx1da=1xlnx.\mathbb{E}[a \mid X = x] = \int_x^1 a\, f(a \mid x)\, da = \frac{1}{-\ln x} \int_x^1 da = \frac{1 - x}{-\ln x}. For the reading x=12x = \tfrac12, where ln12=ln2-\ln\tfrac12 = \ln 2, this comes to E ⁣[aX=12]=1/2ln2= 12ln20.7213. \mathbb{E}\!\left[a \mid X = \tfrac12\right] = \frac{1/2}{\ln 2} = \boxed{\ \frac{1}{2\ln 2} \approx 0.7213.\ } The reading holds the ceiling above 0.50.5, and the pull toward smaller ceilings then drags the average down from the midpoint 0.750.75 of the surviving range, settling at 0.7210.721 (Figure, left).

Another way. The tilt 1/a1/a carries a tidy reading of its own. Setting u=lnau = \ln a turns the weight da/ada/a into plain dudu, so once the reading is in hand lna\ln a is spread evenly between lnx\ln x and 00. The ceiling is therefore a=x1Va = x^{\,1 - V} for VV uniform on [0,1][0,1], sliding from the floor xx up to 11 as VV runs from 00 to 11, and averaging over VV returns the same value, E[ax]=01x1vdv=1xlnx,\mathbb{E}[a \mid x] = \int_0^1 x^{\,1 - v}\, dv = \frac{1 - x}{-\ln x}, which is the mean of a log-uniform value: the plain gap 1x1 - x over the logarithmic gap lnx-\ln x.

Extra credit

This time the reading itself is hidden, and all we are told is that it landed below 0.50.5. The same machinery carries over, except that the evidence is now a whole event rather than one value, so each ceiling is judged by how often it lands there. A calculator with ceiling a12a \le \tfrac12 never climbs past 12\tfrac12, so it falls below for certain; one with a>12a > \tfrac12 manages it only part of the time, with probability 12/a\tfrac12 / a, the share of its range that lies under 12\tfrac12. Writing c=12c = \tfrac12 and weighting the flat prior by this chance gives the posterior f(a){1,0<ac,ca,c<a<1.f(a) \propto \begin{cases} 1, & 0 < a \le c,\\[4pt] \dfrac{c}{a}, & c < a < 1. \end{cases} Its total is the overall chance of a low reading, Z=0c1da+c1cada=c+c(lnc)=c(1lnc),Z = \int_0^c 1\, da + \int_c^1 \frac{c}{a}\, da = c + c(-\ln c) = c\,(1 - \ln c), and the same integral carrying an extra factor of aa gives the unnormalised mean, M=0cada+c1acada=c22+c(1c)=cc22.M = \int_0^c a\, da + \int_c^1 a \cdot \frac{c}{a}\, da = \frac{c^2}{2} + c(1 - c) = c - \frac{c^2}{2}. Their ratio is the answer, E[aX<c]=MZ=cc2/2c(1lnc)=1c/21lnc,\mathbb{E}[a \mid X < c] = \frac{M}{Z} = \frac{c - c^2/2}{c\,(1 - \ln c)} = \frac{1 - c/2}{1 - \ln c}, and at c=12c = \tfrac12 it works out to E ⁣[aX<12]=3/41+ln2= 34(1+ln2)0.4430. \mathbb{E}\!\left[a \mid X < \tfrac12\right] = \frac{3/4}{1 + \ln 2} = \boxed{\ \frac{3}{4\,(1 + \ln 2)} \approx 0.4430.\ } The two halves of the puzzle send the same number 0.50.5 in opposite directions. Seeing it exactly proves the ceiling reaches that high and lifts the estimate to 0.7210.721, whereas learning only that the reading stayed below it points to a low ceiling, since those are the machines bound to land there, and the estimate falls to 0.4430.443 (Figure, right).

Another way. The extra credit also drops out of the main answer, by averaging it over the reading we never saw. A reading near xx turns up with density f(x)=x1daa=lnxf(x) = \int_x^1 \frac{da}{a} = -\ln x across (0,1)(0,1), so the law of total expectation gives E[aX<c]=0cE[aX=x]f(x)dx0cf(x)dx.\mathbb{E}[a \mid X < c] = \frac{\displaystyle\int_0^c \mathbb{E}[a \mid X = x]\, f(x)\, dx}{\displaystyle\int_0^c f(x)\, dx}. The frequency lnx-\ln x is the very denominator buried inside E[aX=x]=(1x)/(lnx)\mathbb{E}[a \mid X = x] = (1 - x)/(-\ln x), so the two cancel and leave E[aX<c]=0c(1x)dx0c(lnx)dx=cc2/2c(1lnc),\mathbb{E}[a \mid X < c] = \frac{\int_0^c (1 - x)\, dx}{\int_0^c (-\ln x)\, dx} = \frac{c - c^2/2}{c\,(1 - \ln c)}, the same value once more.

The posterior density of the bound aa. Left: an exact reading of 0.50.5 rules out every a<12a < \tfrac12 and leaves a 1/a1/a tail, mean 12ln20.721\tfrac{1}{2\ln 2} \approx 0.721. Right: knowing only that the reading fell below 0.50.5 keeps the small bounds at full weight (flat part) and trims the large ones, mean 34(1+ln2)0.443\tfrac{3}{4(1+\ln 2)} \approx 0.443. The copper line marks each mean.

The computation

The check simulates the broken calculator rather than the formula: draw a bound aa uniformly on (0,1)(0, 1), draw a reading uniformly on (0,a)(0, a), and keep the runs that match what was observed; the average bound over those runs is the answer. An exact reading of 0.50.5 has probability zero, so we keep readings inside a thin window and watch the estimate settle as the window shrinks; the event “below 0.50.5” has positive probability, so plain accept-and-average is exact.

import random
from math import log
random.seed(0)

# Main: the reading came out exactly 0.5. Condition on a thin window |x-0.5|<eps;
# as the window shrinks the estimate approaches E[a | X = 0.5].
def estimate_exact(eps, N=20_000_000):
    total = count = 0
    for _ in range(N):
        a = random.random()          # prior:  a ~ Uniform(0, 1)
        x = random.random() * a      # broken: x ~ Uniform(0, a)
        if abs(x - 0.5) < eps:
            total += a; count += 1
    return total / count

for eps in (0.02, 0.01, 0.004):
    print(f"exact reading, window {eps:<5}:  E[a] ~ {estimate_exact(eps):.4f}")
print(f"closed form 1/(2 ln 2)        =  {1 / (2 * log(2)):.4f}")

# Extra credit: the reading is only known to be below 0.5. A finite-probability
# event, so a plain accept/reject Monte Carlo is exact.
def estimate_below(N=40_000_000):
    total = count = 0
    for _ in range(N):
        a = random.random()
        x = random.random() * a
        if x < 0.5:
            total += a; count += 1
    return total / count

print(f"reading below 0.5:  E[a] ~ {estimate_below():.4f}")
print(f"closed form (3/4)/(1+ln 2) = {0.75 / (1 + log(2)):.4f}")
# exact reading, window 0.02 :  E[a] ~ 0.7210
# exact reading, window 0.01 :  E[a] ~ 0.7211
# exact reading, window 0.004:  E[a] ~ 0.7209
# closed form 1/(2 ln 2)        =  0.7213
# reading below 0.5:  E[a] ~ 0.4429
# closed form (3/4)/(1+ln 2) = 0.4430

The two alternate derivations are identities, not experiments, so a symbolic check suits them: the log-uniform mean 01x1vdv\int_0^1 x^{1-v}\,dv for the main puzzle, and the total-expectation quotient for the extra credit, with the marginal f(x)=lnxf(x) = -\ln x computed rather than assumed.

import sympy as sp
x, v, a, c = sp.symbols("x v a c", positive=True)

# Main, log-uniform route: given the reading x, ln(a) is uniform, so a = x**(1-V),
# V uniform on [0, 1]. The x < 1 branch of its mean is (x-1)/ln(x):
main = sp.integrate(x ** (1 - v), (v, 0, 1))
print("main  E[a] = (x-1)/ln x ;  at x=1/2 ->",
      float(((x - 1) / sp.log(x)).subs(x, sp.Rational(1, 2))))

# Extra credit, law of total expectation over the hidden reading.
fX = sp.integrate(1 / a, (a, x, 1))             # marginal of the reading = -ln x
Emean = (x - 1) / sp.log(x)                      # = E[a | X = x]
tower = sp.simplify(sp.integrate(Emean * fX, (x, 0, c)) / sp.integrate(fX, (x, 0, c)))
print("EC    E[a|X<c] =", tower, "; at c=1/2 ->",
      float(tower.subs(c, sp.Rational(1, 2))))
# main  E[a] = (x-1)/ln x ;  at x=1/2 -> 0.7213475204444817
# EC    E[a|X<c] = (c - 2)/(2*(log(c) - 1)) ; at c=1/2 -> 0.44296208186223096
Interactive — watch the belief update

E[a] = (1 − x) / (−ln x) = 0.721

The belief in the bound a, before any reading flat across 0 to 1. An exact reading rules out every a below it and leaves a 1/a tail; a reading only known to fall below the mark keeps the small bounds at full weight. Drag the value, or switch what you learned, and the copper line tracks the new average. At 0.5 the two readings give the chapter's answers, 0.721 and 0.443.