How can I generate non-repetitive random numbers in numpy?
list = np.random.random_integers(20,size=(10))
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
numpy.random.Generator.choiceoffers areplaceargument to sample without replacement:If you’re on a pre-1.17 NumPy, without the
GeneratorAPI, you can userandom.sample()from the standard library:You can also use
numpy.random.shuffle()and slicing, but this will be less efficient:There’s also a
replaceargument in the legacynumpy.random.choicefunction, 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:
So
numpy.random.Generator.choiceis what you usually want to go for, except for very small output size/k.