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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:07:17+00:00 2026-06-12T10:07:17+00:00

I’ve been using the random_element() function provided by SAGE to generate random integer partitions

  • 0

I’ve been using the random_element() function provided by SAGE to generate random integer partitions for a given integer (N) that are a particular length (S). I’m trying to generate unbiased random samples from the set of all partitions for given values of N and S. SAGE’s function quickly returns random partitions for N (i.e. Partitions(N).random_element()).

However, it slows immensely when adding S (i.e. Partitions(N,length=S).random_element()). Likewise, filtering out random partitions of N that are of length S is incredibly slow.

However, and I hope this helps someone, I’ve found that in the case when the function returns a partition of N not matching the length S, that the conjugate partition is often of length S. That is:

S = 10
N = 100
part = list(Partitions(N).random_element())
    if len(part) != S:
        SAD = list(Partition(part).conjugate())
        if len(SAD) != S:
            continue

This increases the rate at which partitions of length S are found and appears to produce unbiased samples (I’ve examined the results against entire sets of partitions for various values of N and S).

However, I’m using values of N (e.g. 10,000) and S (e.g. 300) that make even this approach impractically slow. The comment associated with SAGE’s random_element() function admits there is plenty of room for optimization. So, is there a way to more quickly generate unbiased (i.e. random uniform) samples of integer partitions matching given values of N and S, perhaps, by not generating partitions that do not match S? Additionally, using conjugate partitions works well in many cases to produce unbiased samples, but I can’t say that I precisely understand why.

  • 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-12T10:07:18+00:00Added an answer on June 12, 2026 at 10:07 am

    Finally, I have a definitively unbiased method that has a zero rejection rate. Of course, I’ve tested it to make sure the results are representative samples of entire feasible sets. It’s very fast and totally unbiased. Enjoy.

    from sage.all import *
    import random
    

    First, a function to find the smallest maximum addend for a partition of n with s parts

    def min_max(n,s):
    
        _min = int(floor(float(n)/float(s)))
        if int(n%s) > 0:
            _min +=1
    
        return _min
    

    Next, A function that uses a cache and memoiziation to find the number of partitions
    of n with s parts having x as the largest part. This is fast, but I think there’s
    a more elegant solution to be had. e.g., Often: P(N,S,max=K) = P(N-K,S-1)
    Thanks to ante (https://stackoverflow.com/users/494076/ante) for helping me with this:
    Finding the number of integer partitions given a total, a number of parts, and a maximum summand

    D = {}
    def P(n,s,x):
        if n > s*x or x <= 0: return 0
        if n == s*x: return 1
        if (n,s,x) not in D:
            D[(n,s,x)] = sum(P(n-i*x, s-i, x-1) for i in xrange(s))
        return D[(n,s,x)]
    

    Finally, a function to find uniform random partitions of n with s parts, with no rejection rate! Each randomly chosen number codes for a specific partition of n having s parts.

    def random_partition(n,s):
        S = s
        partition = []
        _min = min_max(n,S)
        _max = n-S+1
    
        total = number_of_partitions(n,S)
        which = random.randrange(1,total+1) # random number
    
        while n:
            for k in range(_min,_max+1):
                count = P(n,S,k)
                if count >= which:
                    count = P(n,S,k-1)
                    break
    
            partition.append(k)
            n -= k
            if n == 0: break
            S -= 1
            which -= count
            _min = min_max(n,S)
            _max = k
    
        return partition
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.