Skip to content

Instantly share code, notes, and snippets.

@dav-s
Created February 16, 2016 19:52
Show Gist options
  • Save dav-s/ebfcdd773d8a89e19fca to your computer and use it in GitHub Desktop.
Save dav-s/ebfcdd773d8a89e19fca to your computer and use it in GitHub Desktop.
# Davis Robertson
import random
# 0 Spades
# 1 Clubs
# 2 Hearts
# 3 Diamonds
# Used if cards need to be represented as strings
suits = "S C H D".split(" ")
values = "A 2 3 4 5 6 7 8 9 X J Q K".split(" ")
def get_card():
return random.randint(0, 51)
def get_suit(card):
return card % 4
def get_value(card):
return card / 4
def is_facecard(card):
return get_value(card) >= 10
def to_string(card):
return values[get_value(card)]+suits[get_suit(card)]
# Name of the csv goes here
# Saves as a csv so it is easier to deal with in excel, sheets, etc
f = open("data.csv", "w")
f.write("Draws until face card,Cards drawn\n")
# Number of trails here
for i in range(10000):
hand = []
while True:
card = get_card()
hand.append(str(card))
# Used if replacement is not used
# while card in hand:
# card = get_card()
if is_facecard(card):
break
# Writes the number of cards drawn in the first column
# and the cards drawn in the following columns
f.write(str(len(hand)) + "," + ",".join(hand) + "\n")
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment