This program calculates the number of flushes in a number of deals of a poker hand. You can tell it to deal say 10,000 hands and see how many were flushes. This is basically my first Python program.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ```
import time
import random
#deal poker hand and see if you get a flush
print "This simulates poker hands"
flush=0
n=int(raw_input("no of hands="))
t1=time.clock() #start the clock ticking
for i in range(n):#deal n hands
count1=count2=count3=count4=0 # set counters to zero
for i in range(0,5):#deal 5 cards
card=random.choice(['ace',2,3,4,5,6,7,8,9,10,'jack','queen','king'])
#but what suit is it?
suit=random.choice(['spades','diamonds','hearts','clubs'])
if suit=='spades':
count1+=1
elif suit=="diamonds":
count2+=1
elif suit=="hearts":
count3+=1
elif suit=="clubs":
count4+=1
#print card, suit,count1,count2,count3,count4,flush
#print "---------------------"
if count1==5 or count2==5 or count3==5 or count4==5:
flush=flush+1
print "number of flushes=",flush
t2=time.clock()#stop the clock
process=round(t2-t1,2)#time it took to process commands
prob=float(flush)/float(n) #the probability of getting a flush
print "prob of flush=",prob
print "processor time=",process,
print "secs"
``` |

I wrote this program to see what the probability of getting a flush from a poker hand. It is also written so a beginner can understand it (hopefully) and get some idea how to write a simple program like this one. Obviously there are more efficient ways to code this but I don't have the experience yet to recode it. Well, all the best.

Don't worry about the #print card, suit, ... etc as I used them to "see" what was going on within the program - you can delete them if you want.

Your program can run independently of line 12. Thus it is not taken into account which card was drawn. Or am I missing something.

yes your quite right Fernando. I could delete that line but left it in, in case somebody wanted to (say) calculate a straight somehow.

Point is that you can draw say for example 2 of clubs twice etc. in the same hand?

True, I should have done it without replacement. Thanks for pointing out that error. Thank goodness line 12 is redundant code for the flush.

A minor comment on line 11:

I would use for i in range(5):#deal 5 cards

instead of for i in range(0,5):#deal 5 cards

After you use Python for a while, I think you'll find that is simpler.

But, your algorithm is wrong as Fernando pointed out. You do need line 12 or something like it, and you need to check to make sure that you don't have repeats for one hand anyway. So, for a hand, you need to check for the second, third, fourth and fifth cards that they are not repeats. After you deal a card, there are now only 12 cards left in that suit, etc, there is not still 13. So, your algorithm is making it a little too easy to get a flush.

Also, in effect, you are reshuffling after each hand. The probablities should be the same for every hand. Yoo are counting the actual number of flushes that you get when you repeatedly deal one hand after reshuffling. Over time, the number of actual flushed should converge to the probablity, but with low numbers, the actual results could be way off.

Thanks Robert for your helpful feedback. Yes, I'll have to correct the code somewhat.