from itertools import * # probabilities of various events when drawing balls at random without # replacement from a shuffled urn with r red and b blue balls def red_blue(red, blue, k): total = 0 no_red = 0 at_least_one_red = 0 one_of_each = 0 same_number = 0 for arrangement in combinations(range(red + blue), red): total = total + 1 # count the number of red balls among the first k red_count = count_reds(arrangement, k) # update the counters depending on the value if red_count == 0: no_red = no_red + 1 else: at_least_one_red = at_least_one_red + 1 if red_count < k: one_of_each = one_of_each + 1 if 2 * red_count == k: same_number = same_number + 1 return (no_red * 1.0 / total, at_least_one_red * 1.0 / total, one_of_each * 1.0 / total, same_number * 1.0 / total) # determine the number of red balls among the first k in an arrangement def count_reds(arrangement, k): red_count = 0 for pos in arrangement: if pos < k: red_count = red_count + 1 return red_count