Ratios of random numbers

(0 comments)

The last episode of 3Blue1Brown's "lockdown math" series posed the problem: given two numbers, $x$ and $y$, chosen randomly from the uniform distribution between 0 and 1, what is the proportion that their ratio, when rounded down to the nearest integer, is even? That is, what is $P(\lfloor \frac{x}{y} \rfloor \mod 2 = 0)$?

The answer, $1 - \frac{1}{2}\ln 2$, is nicely derived in the video; the following script estimates this value by simulation and visualizes the results on the unit square: the probability is the total area of the red triangles: $$ \frac{1}{2}\left[ 1 + \frac{1}{2} - \frac{1}{3} + \frac{1}{4} - \frac{1}{5} + \cdots \right] = \frac{1}{2}\left[ 1 + 1 - \ln 2 \right] $$

enter image description here

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc('font', family='serif', size=16)


N = 1000000
x = np.random.random(N)
y = np.random.random(N)
r_even = np.floor(x / y) % 2 == 0
x_e, y_e = x[r_even], y[r_even]
x_o, y_o = x[~r_even], y[~r_even]

print('Estimated: {:.6f}'.format(r_even.mean()))
print('    Exact: {:.6f}'.format((2 - np.log(2)) / 2))

DPI = 72
fig, ax = plt.subplots(figsize=(700/DPI, 700/DPI), dpi=DPI)
ax.scatter(x_e, y_e, s=0.1, c='tab:red')
ax.scatter(x_o, y_o, s=0.1, c='tab:blue')
ax.axis('equal')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.savefig('random-ratios.png', dpi=DPI)
plt.show()

Output:

Estimated: 0.653451
    Exact: 0.653426
Current rating: 3.4

Comments

Comments are pre-moderated. Please be patient and your comment will appear soon.

There are currently no comments

New Comment

required

required (not published)

optional

required