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 6332475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:16:17+00:00 2026-05-24T18:16:17+00:00

I’m working on a project for fun and I need an algorithm to do

  • 0

I’m working on a project for fun and I need an algorithm to do as follows:
Generate a list of numbers of Length n which add up to x

I would settle for list of integers, but ideally, I would like to be left with a set of floating point numbers.

I would be very surprised if this problem wasn’t heavily studied, but I’m not sure what to look for.

I’ve tackled similar problems in the past, but this one is decidedly different in nature. Before I’ve generated different combinations of a list of numbers that will add up to x. I’m sure that I could simply bruteforce this problem but that hardly seems like the ideal solution.

Anyone have any idea what this may be called, or how to approach it? Thanks all!

Edit: To clarify, I mean that the list should be length N while the numbers themselves can be of any size.

edit2: Sorry for my improper use of ‘set’, I was using it as a catch all term for a list or an array. I understand that it was causing confusion, my apologies.

  • 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-24T18:16:17+00:00Added an answer on May 24, 2026 at 6:16 pm

    This is how to do it in Python

    import random
    
    def random_values_with_prescribed_sum(n, total):
        x = [random.random() for i in range(n)]
        k = total / sum(x)
        return [v * k for v in x]
    

    Basically you pick n random numbers, compute their sum and compute a scale factor so that the sum will be what you want it to be.

    Note that this approach will not produce “uniform” slices, i.e. the distribution you will get will tend to be more “egalitarian” than it should be if it was picked at random among all distribution with the given sum.

    To see the reason you can just picture what the algorithm does in the case of two numbers with a prescribed sum (e.g. 1):

    enter image description here

    The point P is a generic point obtained by picking two random numbers and it will be uniform inside the square [0,1]x[0,1]. The point Q is the point obtained by scaling P so that the sum is required to be 1. As it’s clear from the picture the points close to the center of the have an higher probability; for example the exact center of the squares will be found by projecting any point on the diagonal (0,0)-(1,1), while the point (0, 1) will be found projecting only points from (0,0)-(0,1)… the diagonal length is sqrt(2)=1.4142... while the square side is only 1.0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.