Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #find out and test the chance how many duplicate card player draw in a card game
- #Given the card deck size and how many 3 card duplicate and 2 card duplicate exist in deck
- import math
- import random
- deck_size = 40 #Number of cards in the pile
- draw_size = 5 #Number of cards we are drawing OR cards in opening hand
- duplication_group_size = {2:5,3:5} #Number of groups with 2 copies of the same cards or 3 copies of the same cards
- #{2:5,3:4} means 5 cards with 2 copies of itself are in the deck and 4 cards with 3 copies of itself are in the deck
- test_sample_size= 1000000 # random test sample size
- #TODO parameter validation
- #calculate theoretical chance to draw no duplicate
- def calc_draw_unique(deck_size,draw_size ,duplication_group_size):
- unique_card__total_size = deck_size - 2 * duplication_group_size[2] - 3 * duplication_group_size[3]
- number_no_duplication_case = 0
- # Only count cases where every cards in hand are unique.
- # out of 5 unique card I draw, some are those with 3 copies of itself in deck. The number of those is n3
- # some are those with 2 copies of itself in deck: The number of those is n2
- # some are unique: The number of those is unique_size
- # in case n2 = 0, number_no_duplication_case = C(5,0)*3**0*C(25,5)+
- # C(5,1)*3**1*C(25,4)+
- # C(5,2)*3**2*C(25,3)+
- # C(5,3)*3**3*C(25,2)+
- # C(5,4)*3**4*C(25,1)+
- # C(5,5)*3**5*C(25,0)
- for n3 in range(0,draw_size+1):
- for n2 in range(0,draw_size-n3+1):
- if n2<=duplication_group_size[2] and n3<=duplication_group_size[3]:
- unique_size = draw_size-n2-n3
- number_no_duplication_case += math.comb(duplication_group_size[3],n3)*3**n3 \
- * math.comb(duplication_group_size[2],n2)*2**n2 \
- * math.comb(unique_card__total_size,unique_size)
- return number_no_duplication_case / math.comb(deck_size, draw_size)
- def create_test_deck(deck_size,duplication_group_size):
- result = {}
- index = 0
- for n in range(0,duplication_group_size[2]): #create cards with 2 copies of itself in deck
- result[index+2*n]='d'+str(n)
- result[index+2*n+1]='d'+str(n)
- index += duplication_group_size[2]*2
- for n in range(0,duplication_group_size[3]): #create cards with 3 copies of itself in deck
- result[index+3*n]='t'+str(n)
- result[index+3*n+1]='t'+str(n)
- result[index+3*n+2]='t'+str(n)
- index += duplication_group_size[3]*3
- for n in range(0,deck_size-duplication_group_size[2]*2-duplication_group_size[3]*3): #create unique cards
- result[index+n]='u'+str(n)
- return result
- def get_random_sample(deck,draw_size):
- return random.sample(sorted(deck.items()), draw_size)
- def has_duplicate(sample):
- sample_list = dict(sample).values()
- return len(sample_list) != len(set(sample_list))
- #main function
- test_deck = create_test_deck(deck_size,duplication_group_size)
- number_test_duplicate = 0
- for n in range(test_sample_size):
- s = get_random_sample(test_deck,draw_size)
- if has_duplicate(s):
- number_test_duplicate+=1
- print ("deck size: ",deck_size)
- print ("draw size: ",draw_size)
- print ("duplication group size: ",duplication_group_size)
- print ("test sample size: ",test_sample_size)
- print("theoretical chance to draw duplicates: ", 1 - calc_draw_unique(deck_size,draw_size,duplication_group_size))
- print("random test result: ", number_test_duplicate/test_sample_size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement