Skip to content

Instantly share code, notes, and snippets.

@evanthebouncy
Created January 18, 2026 08:24
Show Gist options
  • Select an option

  • Save evanthebouncy/896c6302bfe003545497fc9b54e4074b to your computer and use it in GitHub Desktop.

Select an option

Save evanthebouncy/896c6302bfe003545497fc9b54e4074b to your computer and use it in GitHub Desktop.
resolution lower bound
import numpy as np
# generate a random number between 0 and 1
# calculate its distance (min) to either 0 and 1 (absolute value of difference)
def do_once():
x = np.random.rand()
return min(abs(x - 0), abs(x - 1))
# do it many times and get the average value
def do_many_times(n):
return np.mean([do_once() for _ in range(n)])
# generalize do_once to take in a parameter k, if k is 1, the distance is measured to 0/1 and 1/1
# if k is 2, the distance is 0/2, 1/2, 2/2, and if k is 3, 0/3, 1/3, 2/3, 3/3, so on so forth
def do_once(k):
x = np.random.rand()
return min(abs(x - i/k) for i in range(k+1))
# do it many times and get the average value
def do_many_times(n, k):
return np.mean([do_once(k) for _ in range(n)])
print (f'denominator = 1', "resolution lower bound = ", do_many_times(1000000, 1))
print (f'denominator = 2', "resolution lower bound = ", do_many_times(1000000, 2))
print (f'denominator = 3', "resolution lower bound = ", do_many_times(1000000, 3))
print (f'denominator = 4', "resolution lower bound = ", do_many_times(1000000, 4))
print (f'denominator = 5', "resolution lower bound = ", do_many_times(1000000, 5))
print (f'denominator = 6', "resolution lower bound = ", do_many_times(1000000, 6))
print (f'denominator = 7', "resolution lower bound = ", do_many_times(1000000, 7))
print (f'denominator = 8', "resolution lower bound = ", do_many_times(1000000, 8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment