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

  • Home
  • SEARCH
  • 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 7191779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:48:06+00:00 2026-05-28T19:48:06+00:00

I need to distribute a value based on some weights. For example, if my

  • 0

I need to distribute a value based on some weights. For example, if my weights are 1 and 2, then I would expect the column weighted as 2 to have twice the value as the column weighted 1.

I have some Python code to demonstrate what I’m trying to do, and the problem:

def distribute(total, distribution):
    distributed_total = []
    for weight in distribution:
        weight = float(weight)
        p = weight/sum(distribution)
        weighted_value = round(p*total)
        distributed_total.append(weighted_value)
    return distributed_total

for x in xrange(100):
    d = distribute(x, (1,2,3))
    if x != sum(d):
        print x, sum(d), d

There are many cases shown by the code above where distributing a value results in the sum of the distribution being different than the original value. For example, distributing 3 with weights of (1,2,3) results in (1,1,2), which totals 4.

What is the simplest way to fix this distribution algorithm?

UPDATE:

I expect the distributed values to be integer values. It doesn’t matter exactly how the integers are distributed as long as they total to the correct value, and they are “as close as possible” to the correct distribution.

(By correct distribution I mean the non-integer distribution, and I haven’t fully defined what I mean by “as close as possible.” There are perhaps several valid outputs, so long as they total the original value.)

  • 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-28T19:48:07+00:00Added an answer on May 28, 2026 at 7:48 pm

    Distribute the first share as expected. Now you have a simpler problem, with one fewer participants, and a reduced amount available for distribution. Repeat until there are no more participants.

    >>> def distribute2(available, weights):
    ...     distributed_amounts = []
    ...     total_weights = sum(weights)
    ...     for weight in weights:
    ...         weight = float(weight)
    ...         p = weight / total_weights
    ...         distributed_amount = round(p * available)
    ...         distributed_amounts.append(distributed_amount)
    ...         total_weights -= weight
    ...         available -= distributed_amount
    ...     return distributed_amounts
    ...
    >>> for x in xrange(100):
    ...     d = distribute2(x, (1,2,3))
    ...     if x != sum(d):
    ...         print x, sum(d), d
    ...
    >>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some raw voice audio that we need to distribute over the internet.
I have a Total value that I need to distribute among several rows in
I have a need to write code that will prorate a value across a
I need to distribute some sort of static configuration through my application. What is
I need to run some code on a Linux machine with Python 2.3.4 pre-installed.
I need some tangible guidance on implementing list of value objects in my distributed
I need to distribute a set of data evenly over time based on historical
I'm facing a problem when I need to distribute my java application, created using
I need to build a C++ library to distribute among our customers. The library
I need to add a C# solution with examples that would be distributed as

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.