in api/choice_algorithm.py [0:0]
def suggest_participant(wheel):
"""
Suggest a participant given weights of all participants with randomization.
This is weighted selection where all participants start with a weight of 1,
so the sum of the weights will always equal the number of participants
:param wheel: Wheel dictionary:
{
"id": string ID of the wheel (DDB Hash Key),
"name": string name of the wheel,
"participant_count": number of participants in the wheel,
}
:return: ID of the suggested participant
"""
if wheel['participant_count'] == 0:
raise BadRequestError("Cannot suggest a participant when the wheel doesn't have any!")
query_params = {'KeyConditionExpression': Key('wheel_id').eq(wheel['id'])}
participants = WheelParticipant.iter_query(**query_params)
selected_total_weight = random.random() * float(sum([participant['weight'] for participant in participants]))
# We do potentially want to return the last participant just as a safeguard for rounding errors
participant = None
for participant in WheelParticipant.iter_query(**query_params):
selected_total_weight -= float(participant['weight'])
if selected_total_weight <= 0:
return participant['id']
return participant['id']