mercredi 3 mars 2021

Writing a python program to generate the net profits from gambling in a game -- output not consistent with statistics math

This is not for homework -- I just thought it would be interesting to test. I'm taking a stats class, the question prompt is exactly as follows:

An urn contains 7 blue ball and 7 red balls. In this game, two balls are selected from the urn at random; if the selected balls have the same color, you win; otherwise, you lose. To play the game, you pay $5. If you win, you recieve $9. If you lose, then you forfeit the $5 that you paid. In the long run, on average, how much money would you expect to gain or lose by playing this game?

So, The answer is $-0.85/game. If you'd like the proof, lmk. So I thought it would be interesting to test this out in code, but the result I keep getting is actually around $-1.90/game.

Either I'm coding it wrong, random number distributions aren't actually equivalent, or I haven't ran enough test runs (most was 5000 iterations). I have it set up so that if 1-7 is selected, it's considered a red ball. If 8-14 is selected, it's considered a blue ball. Here's my code:

import random

profit = 0
runs = 100

print('outcome for ' + str(runs) + ' runs:  \n')

for i in range(runs):
  ball1 = random.randrange(1,15,1)
  ball2 = random.randrange(1,15,1)

  #Can't be the same number 
  while ball1 == ball2:
    ball2 = random.randrange(1,15,1)

  if ball1 < 8 and ball2 < 8:
    print('two red')
    profit = profit + 4
  if ball1 > 7 and ball2 > 7:
    print('two blue')
    profit = profit + 4
  else:
    print('not same')
    profit = profit - 5

  print('ball1: ' + str(ball1) + ' ball2: ' + str(ball2) + ' \n' + 'profit status: ' + str(profit) + '\n')

print('total profit: ' + str(profit) + '\nprofit/game: ' + str(profit/runs))

Please I would love to hear what you think!




Aucun commentaire:

Enregistrer un commentaire