Python Forum
I am getting an incorrect average, and not sure why? What's wrong with my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting an incorrect average, and not sure why? What's wrong with my code?
#9
I think the averages in your last post look fine. 1000 hands is a pretty small sample size for this game as the number of hands required to win or lose varies greatly. I modified my program slightly to collect the counts and do a little statistical analysis and plotting.
import random
import statistics
import collections
import matplotlib.pyplot as plt
 
deck = 'A234567890JQK'*4
hand = 6
 
def play(start):
    count = 0
    bank = start
    while 0 < bank < start*2:
        if 'A' in random.sample(deck, hand):
            bank += 1
        else:
            bank -= 1
        count += 1
    return count

while True:
    start = int(input('Enter inital amount: '))
    counts = [play(start) for _ in range(1000)]
    unique_counts = collections.Counter(counts)
    print('Mean =', statistics.mean(counts))
    print('Median =', statistics.median(counts))
    print('Number of different counts =', len(unique_counts))
    print('Count range =', min(counts), 'to', max(counts))
    mode = statistics.mode(counts)
    print('Mode =', mode, 'Count =', unique_counts[mode])
    print('Standard Deviation =', statistics.stdev(counts))
    plt.bar(unique_counts.keys(), unique_counts.values())
    plt.show()
Output:
Enter inital amount: 100 Mean = 484.818 Median = 474.0 Number of different counts = 222 Count range = 238 to 952 Mode = 454 Count = 15 Standard Deviation = 103.47417929987066 Enter inital amount: 100 Enter inital amount: 100 Mean = 492.046 Median = 480.0 Number of different counts = 227 Count range = 250 to 934 Mode = 466 Count = 15 Standard Deviation = 102.92227591186895
Running two tests the average count changed from 484 to 492. Such variation is not surprising when you see the number of hands required to win or lose a game ranges from the low 200's to over 1000 upon occasion. When I increased the sample size from 1000 to 10,000 I also noticed some interesting clumping in the plots. The kind of non-uniform distribution that raises hell with statistical analysis.

What I found really interesting is how bad this game is. You never win. I modified the code to count wins and losses and I never win. I guess this makes sense considering you only have a 38% chance of winning any given hand and you need to win a lot of hands to double your money. The odds are not in your favor.
Reply


Messages In This Thread
RE: I am getting an incorrect average, and not sure why? What's wrong with my code? - by deanhystad - Nov-20-2020, 05:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 1,455 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  what is wrong with my code 53535 4 1,664 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 2,340 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 2,763 Dec-09-2020, 06:37 AM
Last Post: stranac
  Something is Wrong with my code susmith552 4 3,123 Nov-28-2019, 02:16 AM
Last Post: susmith552
  What is wrong with my code? Than999 1 2,439 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  Wrong output on my code. JTNA 2 8,024 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Return giving incorrect value Calingaladha 5 3,472 Oct-24-2018, 09:53 PM
Last Post: LeSchakal
  Why is this code wrong? Lemmy 4 5,273 Apr-05-2018, 03:46 PM
Last Post: Lemmy
  What's wrong with my code and visuals for python? beginnercoder04 2 2,904 Mar-17-2018, 01:06 AM
Last Post: beginnercoder04

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020