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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:49:29+00:00 2026-05-27T14:49:29+00:00

How can I generate non-repetitive random numbers in numpy? list = np.random.random_integers(20,size=(10))

  • 0

How can I generate non-repetitive random numbers in numpy?

list = np.random.random_integers(20,size=(10))
  • 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-05-27T14:49:30+00:00Added an answer on May 27, 2026 at 2:49 pm

    numpy.random.Generator.choice offers a replace argument to sample without replacement:

    from numpy.random import default_rng
    
    rng = default_rng()
    numbers = rng.choice(20, size=10, replace=False)
    

    If you’re on a pre-1.17 NumPy, without the Generator API, you can use random.sample() from the standard library:

    print(random.sample(range(20), 10))
    

    You can also use numpy.random.shuffle() and slicing, but this will be less efficient:

    a = numpy.arange(20)
    numpy.random.shuffle(a)
    print a[:10]
    

    There’s also a replace argument in the legacy numpy.random.choice function, but this argument was implemented inefficiently and then left inefficient due to random number stream stability guarantees, so its use isn’t recommended. (It basically does the shuffle-and-slice thing internally.)

    Some timings:

    import timeit
    print("when output size/k is large, np.random.default_rng().choice() is far far quicker, even when including time taken to create np.random.default_rng()")
    print(1, timeit.timeit("rng.choice(a=10**5, size=10**4, replace=False, shuffle=False)", setup="import numpy as np; rng=np.random.default_rng()", number=10**3)) #0.16003450006246567
    print(2, timeit.timeit("np.random.default_rng().choice(a=10**5, size=10**4, replace=False, shuffle=False)", setup="import numpy as np", number=10**3)) #0.19915290002245456
    
    print(3, timeit.timeit("random.sample( population=range(10**5), k=10**4)", setup="import random", number=10**3))   #5.115292700007558
    
    print("when output size/k is very small, random.sample() is quicker")
    print(4, timeit.timeit("rng.choice(a=10**5, size=10**1, replace=False, shuffle=False)", setup="import numpy as np; rng=np.random.default_rng()", number=10**3))  #0.01609779999125749
    print(5, timeit.timeit("random.sample( population=range(10**5), k=10**1)", setup="import random", number=10**3))  #0.008387799956835806
    

    So numpy.random.Generator.choice is what you usually want to go for, except for very small output size/k.

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

Sidebar

Related Questions

Can you tell me any ways to generate non-uniform random numbers? I am using
Possible Duplicates: Unique (non-repeating) random numbers in O(1)? How do you efficiently generate a
I'd like to generate random, non-white colors using hue, saturation and brightness that can
I know you can generate all permutations from a list, using glob or Algorithm::Permute
Does anyone know of a solid non-encumbered suite of XSLT sheets that can generate
Is there a method I can use to generate non-linear Gradient brushes in c#?
By using the android package manager I can generate a list of all installed
please tell me how to generate 5 different non repeatable random number between 1,20
I can generate my models and schema.yml file based on an existing database. But
In Clearcase I can generate a label for a given set of files and

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.