Skip to content

Instantly share code, notes, and snippets.

@RFullum
Created June 26, 2020 10:51
Show Gist options
  • Save RFullum/795e8b51aa4ca7537a034234be881527 to your computer and use it in GitHub Desktop.
Save RFullum/795e8b51aa4ca7537a034234be881527 to your computer and use it in GitHub Desktop.
Two Random Number Guessing Game Simulation: Introducing a third random increases win percentage towards 66.6%
'''
Two Number Guessing Game:
-Player 1 picks two random numbers, A and B. In reality they can be any two real numbers.
In python we run into trouble with numbers larger than 2,147,483,647 due
to 32 bit arithmetic, so we'll bound the limits here.
-Player 2 can see one of the two numbers.
-Player 2 then has to decide which number is larger.
It seems like it would be a random chance. Regardless of what number A is,
there's an infinite amount of numbers greater than, and an infinte amount of
numbers less than that number.
By introducing a third random number K, Player 2 can increase their probability
to greater than 50%.
'''
from numpy import random
#initialize variables
correct = 0
incorrect = 0
mu = 0 #mean: centerpoint of total distribution of random numbers
sigma = 2147483648 * 2 + 1 #range: -2147483648 through 2147483648, technological limit to -inf to inf
#play game range(X) times. Edit X to see how it affects win percentage.
for i in range(100000):
#assign numbers
#player1's numbers:
numberA = random.normal(mu, sigma)
numberB = random.normal(mu, sigma)
playerNumbers = [numberA, numberB]
#player2's number
numberK = random.normal(mu, sigma)
#if A > K, A biggest
#if A < K, B biggest
if numberA > numberK:
choice = numberA
else:
choice = numberB
if choice == max(playerNumbers):
correct += 1
else:
incorrect += 1
#calculate percentage of correct guesses
winPct = (correct / (correct + incorrect)) * 100
print("correct: ", correct, " incorrect: ", incorrect)
print("Win percentage: ", winPct, "%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment