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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:06:14+00:00 2026-05-16T10:06:14+00:00

I hope I can explain this well, if I don’t I’ll try again. I

  • 0

I hope I can explain this well, if I don’t I’ll try again.

I want to generate an array of 5 random numbers that all add up to 10 but whose allocation are chosen on an interval of [0,2n/m].

I’m using numpy.

The code I have so far looks like this:

import numpy as np

n=10
m=5
#interval that numbers are generated on
randNumbers= np.random.uniform(0,np.divide(np.multiply(2.0,n),fronts),fronts)
#Here I normalize the random numbers
normNumbers = np.divide(randNumbers,np.sum(randNumbers))
#Next I multiply the normalized numbers by n
newList = np.multiply(normNumbers,n)
#Round the numbers two whole numbers
finalList = np.around(newList)

This works for the most part, however the rounding is off, it will add up to 9 or 11 as opposed to 10. Is there a way to do what I’m trying to do without worrying about rounding errors, or maybe a way to work around them? If you would like for me to be more clear I can, because I have trouble explaining what I’m trying to do with this when talking :).

  • 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-16T10:06:14+00:00Added an answer on May 16, 2026 at 10:06 am

    This generates all the possible combinations that sum to 10 and selects a random one

    from itertools import product
    from random import choice
    n=10
    m=5
    finalList = choice([x for x in product(*[range(2*n/m+1)]*m) if sum(x) == 10])
    

    There may be a more efficient way, but this will select fairly between the outcomes

    Lets see how this works when n=10 and m=5

    2*n/m+1 = 5, so the expression becomes

    finalList = choice([x for x in product(*[range(5)]*5) if sum(x) == 10])
    

    `*[range(5)]*5 is using argument unpacking. This is equivalent to

    finalList = choice([x for x in product(range(5),range(5),range(5),range(5),range(5)) if sum(x) == 10])
    

    product() gives the cartesian product of the parameters, which in this case has 5**5 elements, but we then filter out the ones that don’t add to 10, which leaves a list of 381 values

    choice() is used to select a random value from the resultant list

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

Sidebar

Related Questions

I hope I can explain this well enough so that you can understand the
I hope I can explain this problem decently well! I'm trying to implement a
I hope I can explain this right I have two input fields that require
I hope this I can explain what I am trying to achieve: I want
I hope I can explain this properly. I have a grid that is part
I hope I can explain this properly. I'm making an android app that, when
I hope I can explain this well enough... I've created an xml document with
Now this is tricky... I hope i can explain it well.. I have a
Well guys, this problem just baffles me. I hope someone can explain to me
I hope I can explain this clearly enough. I have my main form (A)

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.