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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:59:23+00:00 2026-05-13T13:59:23+00:00

A Google search reveals plenty about generating all possible partitions of an integer n

  • 0

A Google search reveals plenty about generating all possible partitions of an integer n into m parts, but I haven’t found anything about sampling a uniformly distributed random partition of n into m parts.

  • 1 1 Answer
  • 1 View
  • 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-13T13:59:23+00:00Added an answer on May 13, 2026 at 1:59 pm

    Here is some code that does it. This is O(n2) the first time you call it, but it builds a cache so that subsequent calls are O(n).

    import random
    
    cache = {}
    
    def count_partitions(n, limit):
        if n == 0:
            return 1
        if (n, limit) in cache:
            return cache[n, limit]
        x = cache[n, limit] = sum(count_partitions(n-k, k) for k in range(1, min(limit, n) + 1))
        return x
    
    def random_partition(n):
        a = []
        limit = n
        total = count_partitions(n, limit)
        which = random.randrange(total)
        while n:
            for k in range(1, min(limit, n) + 1):
                count = count_partitions(n-k, k)
                if which < count:
                    break
                which -= count
            a.append(k)
            limit = k
            n -= k
        return a
    

    How this works: We can calculate how many partitions of an integer n there are in O(n2) time. As a side effect, this produces a table of size O(n2) which we can then use to generate the kth partition of n, for any integer k, in O(n) time.

    So let total = the number of partitions. Pick a random number k from 0 to total – 1. Generate the kth partition.

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

Sidebar

Related Questions

I just got this error message in Firebug. A google search reveals nothing but
what I have: a google search form (displayed on all pages) a wordpress page
I did a google search and found some forum threads explaining it won't work
I found this on net in google search and see article here: http://www.thatcssguy.com/limit-your-divs/ See
somehow couldn't find this with a google search, but I feel like it has
So a quick Google search for fflush(stdin) for clearing the input buffer reveals numerous
I've tried every Google search term I can think of but everything I dig
I want to use Google search.But the search results are only from my site,
On a Google Search, I found this article: http://docs.python.org/release/1.4/lib/node201.html Which showed examples of using
I have found a XML file through google search that will help me for

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.