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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:42:03+00:00 2026-06-03T16:42:03+00:00

In another thread, I saw the time complexity of a binary-heap weighted random sample

  • 0

In another thread, I saw the time complexity of a binary-heap weighted random sample is equal to O(n * log(m)) where n is the number of choices and m is the number of nodes to choose from.

I was wondering about the time complexity of an unweighted random sample which is used by Python as random.sample. Is the time complexity simply O(n) or is it something else entirely?

  • 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-03T16:42:06+00:00Added an answer on June 3, 2026 at 4:42 pm

    Python source: random.py (line 267).

    Here’s the relevant bit:

       315             selected = set()
       316             selected_add = selected.add
       317             for i in range(k):
       318                 j = randbelow(n)
       319                 while j in selected:
       320                     j = randbelow(n)
       321                 selected_add(j)
       322                 result[i] = population[j]
    

    It basically “rolls the dice” for a random index into population. If it gets an index that’s already in the set selected, it re-rolls. Rinse, lather and repeat k times (where k is the number of samples you asked for.)

    It appears to be O(n) in the size of the requested number of samples. There are some optimisations for small sets, but the meat of the thing is the main loop above.


    Edit:

    I believe line 305-313 are a special case for when the number of samples requested, k, is a large proportion of the total population n. Instead of rolling for random elements from the entire population (and re-rolling if we collide with an element we already selected), we explicitly maintain a list of elements we have yet to select. We are guaranteed to get a new element every time, but the tradeoff is that we have to maintain the list.

    If I’m interpreting this wrong, feel free to comment below.

       303         result = [None] * k
       304         setsize = 21        # size of a small set minus size of an empty list
       305         if k > 5:
       306             setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets
       307         if n <= setsize:
       308             # An n-length list is smaller than a k-length set
       309             pool = list(population)
       310             for i in range(k):         # invariant:  non-selected at [0,n-i)
       311                 j = randbelow(n-i)
       312                 result[i] = pool[j]
       313                 pool[j] = pool[n-i-1]   # move non-selected item into vacancy
       314         else:
       315             selected = set()
       316             selected_add = selected.add
       317             for i in range(k):
       318                 j = randbelow(n)
       319                 while j in selected:
       320                     j = randbelow(n)
       321                 selected_add(j)
       322                 result[i] = population[j]
       323         return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this bit of code in another thread void foo { int *n
I saw this script in another thread on stack overflow and tried to play
On another thread I saw a answer to a sql problem using Oracle's analytics.
Answering another thread (see stackoverflow: generate css color schemes ) I bumped into the
[Note: There is another thread about this problem but it did not answer the
I originally started this question in another thread, but that thread was sorta, kinda
I want to access a scrollviewer from another thread. Please tell me how to
I am tring to setText in another thread, that is, child thread. But for
As I don't want to hijack another thread here comes my question about mappings.
I want to update a Object in another thread, and then access it in

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.