mercredi 31 mars 2021

How to get a random permutation of integers inside range without permuting the whole array in python?

Say I have a very long array, and I want to return a random permutation of a subset of that array... How do I do that without having to permute the whole array?

The way I can think of is

import numpy
N = 10000000 # total number of elements
n = 20 # number of elements that I am interested in getting from the permutation
x = numpy.random.permutation(numpy.arange(N))[:n] # this permutes the whole array

Basically, I need to select n unique integers greater than or equal to 0 and less than N...

I know it is easy to do that explicitly, but is there a numpy way or a function in any module in python that works like randperm(N,n) in MATLAB?

So far, I've only found python examples that are variations of what I described up here.




Aucun commentaire:

Enregistrer un commentaire