Saturday, June 13, 2015

Project 2 Part 2: Higher / Lower Bot

So I was wondering if I misunderstood the purpose of the project - was it supposed to be an interface for the game or a bot for the game? Well, I decided I'd just do a bot to play the game. And here it is.


import random
def getmax():
    try:
        return int(input("What's the highest number to play? "))
    except SyntaxError:
        print("Enter a integer.")
        return getmax()
min = 1
max = getmax()
truemax = max
turns = 0
toguess = random.randint(min, max)
done = False
while not done:
    turns += 1
    guess = (min + max) / 2
    range = max - min
    print("Guessed " + str(guess))
    if guess == toguess:
        print("I did it!")
        print("Guessed a number from 0 to " + str(truemax) + " in " + str(turns) + " turns.")
        done = True
    elif guess < toguess:
        print("Too low.")
        min += range / 2
    else:
        print("Too high.")
        max -= range / 2
A pastebin of the code is here.
The coolest part to me is how fast it is. It was able to guess with a range from 1 to 10 billion (10 to the power of 10) in 32 turns and with a range from 1 to 10 septillion in 83 turns. This makes sense, since my implementation is essentially a binary tree with 34 levels (since log2 of 10 billion is 33.2.)

This got me curious just how many iterations my solution would take to guess a number, on average. Some numbers are guessed faster than others. Instead of wasting a bunch of my CPU on Cloud9, though, I decided to calculate it by hand.

From guessing a number between one and ten, here's a tree representation of what my solution would do:

                     5
       3                      8
   2      4               7      9
1                     6             10

So, in the case where we're guessing a number between 1 and 10, if a number is picked at random, it'll take (on average) (1(1) + 2(2) + 3(4) + 4(3)) / 10 = 2.9 turns to be guessed.

Using some modified code to loop from 1 to 100, I got an average of 5.9 turns for a number to be guessed.

Going from 1 to 1000, it took 9.1 turns,

In general, it seems like the average number of iterations to guess a number from 1 to n is less than or equal to:

0.92 * log (base 2) (n) A pastebin of the code is here.
The coolest part to me is how fast it is. It was able to guess with a range from 1 to 10 billion (10 to the power of 10) in 32 turns and with a range from 1 to 10 septillion in 83 turns. This makes sense, since my implementation is essentially a binary tree with 34 levels (since log2 of 10 billion is 33.2.)

This got me curious just how many iterations my solution would take to guess a number, on average. Some numbers are guessed faster than others. Instead of wasting a bunch of my CPU on Cloud9, though, I decided to calculate it by hand.

From guessing a number between one and ten, here's a tree representation of what my solution would do:

                     5
       3                      8
   2      4               7      9
1                     6             10

So, in the case where we're guessing a number between 1 and 10, if a number is picked at random, it'll take (on average) (1(1) + 2(2) + 3(4) + 4(3)) / 10 = 2.9 turns to be guessed.

Using some modified code to loop from 1 to 100, I got an average of 5.9 turns for a number to be guessed.

Going from 1 to 1000, it took 9.1 turns,

In general, it seems like the average number of iterations to guess a number from 1 to n is less than or equal to:

0.92 * log (base 2) (n)

No comments:

Post a Comment