Advertisement
avogatro

chance to draw duplicate in card game like Yo-Gi-Oh or Magic

May 20th, 2024 (edited)
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. #find out and test the chance how many duplicate card player draw in a card game
  2. #Given the card deck size and how many 3 card duplicate and 2 card duplicate exist in deck
  3.  
  4. import math
  5. import random
  6. deck_size = 40 #Number of cards in the pile
  7. draw_size = 5  #Number of cards we are drawing OR cards in opening hand
  8. duplication_group_size = {2:5,3:5} #Number of groups with 2 copies of the same cards or 3 copies of the same cards
  9.                                    #{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
  10. test_sample_size= 1000000 # random test sample size
  11. #TODO parameter validation
  12.  
  13. #calculate theoretical chance to draw no duplicate
  14. def calc_draw_unique(deck_size,draw_size ,duplication_group_size):
  15.     unique_card__total_size = deck_size - 2 * duplication_group_size[2] - 3 * duplication_group_size[3]
  16.     number_no_duplication_case = 0
  17.     # Only count cases where every cards in hand are unique.
  18.     # out of 5 unique card I draw, some are those with 3 copies of itself in deck. The number of those is n3
  19.     # some are those with 2 copies of itself in deck: The number of those is n2
  20.     # some are unique: The number of those is unique_size
  21.     # in case n2 = 0, number_no_duplication_case = C(5,0)*3**0*C(25,5)+
  22.     # C(5,1)*3**1*C(25,4)+
  23.     # C(5,2)*3**2*C(25,3)+
  24.     # C(5,3)*3**3*C(25,2)+
  25.     # C(5,4)*3**4*C(25,1)+
  26.     # C(5,5)*3**5*C(25,0)
  27.     for n3 in range(0,draw_size+1):
  28.         for n2 in range(0,draw_size-n3+1):
  29.             if n2<=duplication_group_size[2] and n3<=duplication_group_size[3]:
  30.                 unique_size = draw_size-n2-n3
  31.                 number_no_duplication_case += math.comb(duplication_group_size[3],n3)*3**n3 \
  32.                                               * math.comb(duplication_group_size[2],n2)*2**n2 \
  33.                                               * math.comb(unique_card__total_size,unique_size)
  34.    
  35.    
  36.     return number_no_duplication_case / math.comb(deck_size, draw_size)
  37.  
  38.  
  39.  
  40.  
  41. def create_test_deck(deck_size,duplication_group_size):
  42.     result = {}
  43.     index = 0
  44.     for n in range(0,duplication_group_size[2]): #create cards with 2 copies of itself in deck
  45.         result[index+2*n]='d'+str(n)
  46.         result[index+2*n+1]='d'+str(n)
  47.     index += duplication_group_size[2]*2
  48.     for n in range(0,duplication_group_size[3]): #create cards with 3 copies of itself in deck
  49.         result[index+3*n]='t'+str(n)
  50.         result[index+3*n+1]='t'+str(n)
  51.         result[index+3*n+2]='t'+str(n)
  52.     index += duplication_group_size[3]*3
  53.     for n in range(0,deck_size-duplication_group_size[2]*2-duplication_group_size[3]*3): #create unique cards
  54.         result[index+n]='u'+str(n)
  55.     return result
  56.  
  57. def get_random_sample(deck,draw_size):
  58.     return random.sample(sorted(deck.items()), draw_size)
  59.  
  60. def has_duplicate(sample):
  61.     sample_list = dict(sample).values()
  62.     return len(sample_list) != len(set(sample_list))
  63.  
  64.  
  65. #main function
  66. test_deck = create_test_deck(deck_size,duplication_group_size)
  67. number_test_duplicate = 0
  68. for n in range(test_sample_size):
  69.     s = get_random_sample(test_deck,draw_size)
  70.     if has_duplicate(s):
  71.         number_test_duplicate+=1
  72.  
  73.  
  74. print ("deck size: ",deck_size)
  75. print ("draw size: ",draw_size)
  76. print ("duplication group size: ",duplication_group_size)
  77. print ("test sample size: ",test_sample_size)
  78. print("theoretical chance to draw duplicates: ", 1 - calc_draw_unique(deck_size,draw_size,duplication_group_size))
  79.  
  80. print("random test result: ", number_test_duplicate/test_sample_size)
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement