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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:42:36+00:00 2026-06-14T14:42:36+00:00

This is related to random sampling. I am using random.sample(number,5) to return a list

  • 0

This is related to random sampling. I am using random.sample(number,5) to return a list of random numbers from within a range of numbers contained in numbers. I am using while i < 100 to return one hundred sets of five numbers. To check for duplicates, I am using :

if len(numbers) != len(set(numbers)):

to identify sets with duplicates and following this with random.sample(number,5) to try to do another randomisation to replace the set with duplicates. I seem to get about 8% getting re-randomised ( using a print statement to say which number was duplicated), but about 5% seem to be missed. What am I doing incorrectly? The actual code is as follows:

while i < 100:
    set1 = random.sample(numbers1,5)
if len(set1) != len(set(set1))
    print('duplicate(s) found, random selection repeated')
set1 = random.sample(numbers1,5)

In another routine I am trying to do the same as above, but searching for duplicates in two sets by adding the same, substituting set2 for set1. This gives the same sorts of failures. The set2 routine is indented and placed immediately below the above routine. While i < 100: is not repeated for set2.
I hope that I have explained my problem clearly!!

  • 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-14T14:42:37+00:00Added an answer on June 14, 2026 at 2:42 pm

    There is nothing in your code to stop the second sample from having duplicates. What if you did something like a second while loop?

    while i<100:
        i+=1
        set1 = random.sample(numbers1,5)
        while len(set1) != len(set(set1)):
            print('duplicate(s) found, random selection repeated')
            set1 = random.sample(numbers1,5)
    

    Of course you’re still missing the part of the code that does something… beyond the above it’s difficult to tell what you might need to change without a full code sample.

    EDIT: here is a working version of the code sample from the comments:

    def choose_random(list1,n):
        import random 
        i = 0 
        set_list=[]
        major_numbers=range(1,50) + list1
        print(major_numbers) 
        while i <n: 
            set1 =random.sample(major_numbers,5) 
            set2 =random.sample(major_numbers,2) 
            while len(set(set1)) != len(set1):
                print("Duplicate found at %i"%i)
                print set1
                print("Changing to:")
                set1 =random.sample(major_numbers,5) 
                print set1
            set_list.append([set1,set2])
            i +=1
        return set_list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a difference between generating multiple numbers using a single random number generator
This is related to another open question of mine . While there aren't any
Sampling uniformly at random from an n-dimensional unit simplex is the fancy way to
See this related question on more generic use of the Boost Random library. My
I need to generate quickly lots of random numbers from binomial distributions for dramatically
Problem fixed, see below What I'm doing is this: Generate random number -> store
I'm trying to select a random 10% sampling from a small table. I thought
This question is an extension of this Related question . Taking Derick's advice, I
I found this related question: How do I use composition with inheritance? I would
I have searched here, GooBingHooVista'd the world and read this related question for VS

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.