def determine_if_winner(): print_points() # Pick a winner at random with a single random number. We divide the number # line up like: # # [ a_points | b_points | c_points | ... everything else ... ] # # and then we choose a place on the number line randomly: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # or: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # You can think of this as assigning a range to each player: # # A wins if random is [0, a_points) # B wins if random is [a_points, a_points + b_points) # C wins if random is [a_points + b_points, a_points + b_points + c_points) # no one wins if random is [a_points + b_points + c_points, 1) rnd = util.random() points_so_far = 0 for user, user_points in util.get_user_points().items(): if rnd < 0.00001 * (user_points + points_so_far): raise Exception('%s wins!' % user) points_so_far += user_points print('The game continues.')
def determine_if_winner(): print_points() # Pick a winner at random with a single random number. We divide the number # line up like: # # [ a_points | b_points | c_points | ... everything else ... ] # # and then we choose a place on the number line randomly: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # or: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # You can think of this as assigning a range to each player: # # A wins if random is [0, a_points) # B wins if random is [a_points, a_points + b_points) # C wins if random is [a_points + b_points, a_points + b_points + c_points) # no one wins if random is [a_points + b_points + c_points, 1) # # The number line defaults to a range of 100000. # In order to ensure fairness with large numbers of points, # we extend the line if total points across all players exceed that value. # Relative chance per-player is still preserved. rnd = util.random() all_user_points = util.get_user_points() summed_user_points = [(user, util.total_user_points(user_points)) for user, user_points in all_user_points.items()] # Don't include negative values when summing user points, # since they have no chance to win anyway # There shouldn't be negative values, but just in case... total_points = sum([ user_points for user, user_points in summed_user_points if user_points > 0 ]) scalar = min(0.00001, 1.0 / total_points) points_so_far = 0 print('Probability of winning:') for user, user_points in summed_user_points: print('%s: %.3f%%' % (user, user_points * scalar * 100)) for user, user_points in summed_user_points: if rnd < scalar * (user_points + points_so_far): raise Exception('%s wins!' % user) points_so_far += user_points print('The game continues.')
def should_block(pr): for user, user_points in util.get_user_points().items(): if util.total_user_points(user_points) < 0: raise Exception( 'Would bring user %s points to %s which is negative.' % (user, user_points))
def print_points(): print('Points:') for user, user_points in util.get_user_points().items(): print(' %s: %s' % (user, util.total_user_points(user_points))) for reason in user_points: print(' %s: %s' % (reason, user_points[reason]))
def print_points(): print('Points:') for user, user_points in util.get_user_points().items(): print(' %s: %s' % (user, user_points))