Random large Number guesser

This is a bianary random number guesser that is the most efficent one you can have that I know of.


In [1]:
from random import randrange
from math import log

In [2]:
n = 10**90

In [3]:
p = randrange(10**90)

In [4]:
i = 0
heigh = n
low = 0
guess = n/2
max_steps = log(n,2) #log base 2
while guess != p:
    if p < guess:
        heigh = guess
        guess = (low+heigh)/2
    if p > guess:
        low = guess
        guess = (low+heigh)/2
    if i > max_steps:
        break
    i += 1
print guess-p, i


0 221

You can prove that the max number of steps for a number n is log2(n) for this number thats 300


In [ ]: