Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:06:53+00:00 2026-05-27T23:06:53+00:00

See update below… I’m writing a Python simulation that assigns an arbitrary number of

  • 0

See update below…

I’m writing a Python simulation that assigns an arbitrary number of imaginary players one goal from an arbitrary pool of goals. The goals have two different levels or proportions of scarcity, prop_high and prop_low, at approximately a 3:1 ratio.

For example, if there are 16 players and 4 goals, or 8 players and 4 goals, the two pools of goals would look like this:

{'A': 6, 'B': 6, 'C': 2, 'D': 2}
{'A': 3, 'B': 3, 'C': 1, 'D': 1}

…with goals A and B occurring 3 times as often as C and D. 6+6+2+2 = 16, which corresponds to the number of players in the simulation, which is good.

I want to have a pool of goals equal to the number of players and distributed so that there are roughly three times as many prop_high goals as there are prop_low goals.

What’s the best way to build an allocation algorithm according to a rough or approximate ratio—something that can handle rounding?

Update:

Assuming 8 players, here’s how the distributions from 2 to 8 goals should hopefully look (prop_high players are starred):

    A   B   C   D   E   F   G   H
2   6*  2                       
3   6*  1   1                   
4   3*  3*  1   1               
5   3*  2*  1   1   1           
6   2*  2*  1*  1   1   1       
7   2*  1*  1*  1   1   1   1   
8   1*  1*  1*  1*  1   1   1   1

These numbers don’t correspond to players. For example, with 5 goals and 8 players, goals A and B have a high proportion in the pool (3 and 2 respectively) while goals C, D, and E are more rare (1 each).

When there’s an odd number of goals, the last of the prop_high gets one less than the others. As the number of goals approaches the number of players, each of the prop_high items gets one less until the end, when there is one of each goal in the pool.

What I’ve done below is assign quantities to the high and low ends of the pool and then make adjustments to the high end, subtracting values according to how close the number of goals is to the number of players. It works well with 8 players (the number of goals in the pool is always equal to 8), but that’s all.

I’m absolutely sure there’s a better, more Pythonic way to handle this sort of algorithm, and I’m pretty sure it’s a relatively common design pattern. I just don’t know where to start googling to find a more elegant way to handle this sort of structure (instead of the brute force method I’m using for now)

import string
import math
letters = string.uppercase

num_players = 8
num_goals = 5
ratio = (3, 1)

prop_high = ratio[0] / float(sum(ratio)) / (float(num_goals)/2)
prop_low = ratio[1] / float(sum(ratio)) / (float(num_goals)/2)

if num_goals % 2 == 1:
    is_odd = True
else:
    is_odd = False

goals_high = []
goals_low = []
high = []
low = []

# Allocate the goals to the pool. Final result will be incorrect.
count = 0
for i in range(num_goals):
    if count < num_goals/2:         # High proportion
        high.append(math.ceil(prop_high * num_players))
        goals_high.append(letters[i])
    else:                           # Low proportion
        low.append(math.ceil(prop_low * num_players))
        goals_low.append(letters[i])
    count += 1


# Make adjustments to the pool allocations to account for rounding and odd numbers
ratio_high_total = len(high)/float(num_players)
overall_ratio = ratio[1]/float(sum(ratio))
marker = (num_players / 2) + 1
offset = num_goals - marker

if num_players == num_goals:
    for i in high:
        high[int(i)] -= 1
elif num_goals == 1:
    low[0] = num_players
elif ratio_high_total == overall_ratio and is_odd:
    high[-1] -= 1
elif ratio_high_total >= overall_ratio:         # Upper half of possible goals
    print offset

    for i in range(offset):
        index = -(int(i) + 1)
        high[index] -= 1

goals = goals_high + goals_low
goals_quantities = high + low


print "Players:", num_players
print "Types of goals:", num_goals
print "Total goals in pool:", sum(goals_quantities)
print "High pool:", goals_high, high
print "Low pool:", goals_low, low
print goals, goals_quantities
print "High proportion:", prop_high, " || Low proportion:", prop_low
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:06:53+00:00Added an answer on May 27, 2026 at 11:06 pm

    Rather than try to get the fractions right, I’d just allocate the goals one at a time in the appropriate ratio. Here the ‘allocate_goals’ generator assigns a goal to each of the low-ratio goals, then to each of the high-ratio goals (repeating 3 times). Then it repeats. The caller, in allocate cuts off this infinite generator at the required number (the number of players) using itertools.islice.

    import collections
    import itertools
    import string
    
    def allocate_goals(prop_low, prop_high):
        prop_high3 = prop_high * 3
        while True:
            for g in prop_low:
                yield g
            for g in prop_high3:
                yield g
    
    def allocate(goals, players):
        letters = string.ascii_uppercase[:goals]
        high_count = goals // 2
        prop_high, prop_low = letters[:high_count], letters[high_count:]
        g = allocate_goals(prop_low, prop_high)
        return collections.Counter(itertools.islice(g, players))
    
    for goals in xrange(2, 9):
        print goals, sorted(allocate(goals, 8).items())
    

    It produces this answer:

    2 [('A', 6), ('B', 2)]
    3 [('A', 4), ('B', 2), ('C', 2)]
    4 [('A', 3), ('B', 3), ('C', 1), ('D', 1)]
    5 [('A', 3), ('B', 2), ('C', 1), ('D', 1), ('E', 1)]
    6 [('A', 2), ('B', 2), ('C', 1), ('D', 1), ('E', 1), ('F', 1)]
    7 [('A', 2), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ('G', 1)]
    8 [('A', 1), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ('G', 1), ('H', 1)]
    

    The great thing about this approach (apart from, I think, that it’s easy to understand) is that it’s quick to turn it into a randomized version.

    Just replace allocate_goals with this:

    def allocate_goals(prop_low, prop_high):
        all_goals = prop_low + prop_high * 3
        while True:
            yield random.choice(all_goals)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Important: See update below.) I'm trying to write a function, import_something , that will
Update: Solved, with code I got it working, see my answer below for the
ORIGINAL (see UPDATED QUESTION below) I am designing a new laboratory database that tests
I see that EF can update a model based on an existing database schema.
Update: After some more reading I see that this problem is totally general, you
Figured it out, see Update below. I'm trying to work with a particular web
UPDATE - I'm probably being daft, see my last update below. I just did
See update below. This is driving me mad. I have followed all the instructions
Update: Please see below; I have removed all abstraction and created a plain PHP
Update - Please see update below. I'm attempting to improve the performance of our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.