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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:51:54+00:00 2026-05-19T14:51:54+00:00

I need a sample, without replacement, from among all possible tuples of numbers from

  • 0

I need a sample, without replacement, from among all possible tuples of numbers from range(n). That is, I have a collection of (0,0), (0,1), …, (0,n), (1,0), (1,1), …, (1,n), …, (n,0), (n,1), (n,n), and I’m trying to get a sample of k of those elements. I am hoping to avoid explicitly building this collection.

I know random.sample(range(n), k) is simple and efficient if I needed a sample from a sequence of numbers rather than tuples of numbers.

Of course, I can explicitly build the list containing all possible (n * n = n^2) tuples, and then call random.sample. But that probably is not efficient if k is much smaller than n^2.

I am not sure if things work the same in Python 2 and 3 in terms of efficiency; I use Python 3.

  • 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-19T14:51:54+00:00Added an answer on May 19, 2026 at 2:51 pm

    Depending on how many of these you’re selecting, it might be simplest to just keep track of what things you’ve already picked (via a set) and then re-pick until you get something that you haven’t picked already.

    The other option is to just use some simple math:

    numbers_in_nxn = random.sample(range(n*n), k) # Use xrange in Python 2.x
    tuples_in_nxn = [divmod(x,n) for x in numbers_in_nxn]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need it so badly I have been trying it from 3 days but
Have words from OCR and need a list of close matches. Can live without
Someone have any sample of Comet app .net? I need one sample how to
Hi I need to downsample a wav audio file's sample rate from 44.1kHz to
1I have the following two tables (sample data) and need to be able to
I need to know, based on the regex itself (without any sample data), what
I am doing a PHP web site, without using any framework. I need that
I need a number of unique random permutations of a list without replacement, efficiently.
I need to create a form (using CreateWindow functions) without any help from the
I often have text in columns and need to replace some things without clobbering

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.