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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:30:10+00:00 2026-06-03T02:30:10+00:00

If I need to divide for example 7 into random number of elements of

  • 0

If I need to divide for example 7 into random number of elements of random size, how would I do this?

So that sometimes I would get [3,4], sometimes [2,3,1] and sometimes [2,2,1,1,0,1]?

I guess it’s quite simple, but I can’t seem to get the results. Here what I am trying to do code-wise (does not work):

def split_big_num(num):
    partition = randint(1,int(4))
    piece = randint(1,int(num))
    result = []
    for i in range(partition):
        element = num-piece
        result.append(element)
        piece = randint(0,element)
#What's next?
        if num - piece == 0:
            return result
    return result

EDIT: Each of the resulting numbers should be less than initial number and the number of zeroes should be no less than number of partitions.

  • 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-06-03T02:30:11+00:00Added an answer on June 3, 2026 at 2:30 am

    I’d go for the next:

    >>> def decomposition(i):
            while i > 0:
                n = random.randint(1, i)
                yield n
                i -= n
    
    >>> list(decomposition(7))
    [2, 4, 1]
    >>> list(decomposition(7))
    [2, 1, 3, 1]
    >>> list(decomposition(7))
    [3, 1, 3]
    >>> list(decomposition(7))
    [6, 1]
    >>> list(decomposition(7))
    [5, 1, 1]
    

    However, I am not sure if this random distribution is perfectly uniform.

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

Sidebar

Related Questions

I need to divide a C string into tokens. I thought that strtok will
I have a list of MyObjects which I need to divide into three groups:
I have a byte array: byte[] bytes; // many elements I need to divide
I need to divide one int into 2 other int's. the first int is
I have this text file that I read into a Java application and then
I'm working on a micro-controller without hardware multiply and divide. I need to cook
I need to access variables in whole script that is divided to few sections
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Greetings, Any input on a way to divide a std::vector into two equal parts

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.