Randomly sampling a given number of permutations with ot.KPermutations

Hello,
I was wondering whether there is way to randomly sample a given number n of permutations of a vector when using the ot.KPermutations class, without having to generate the entire set of permutations.
For instance, if I want to generate 50 permutations of the vector containing integers ranging from 0 to 99, this cannot be done with ot.KPermutations(100).generate() as the total number of permutations to be computed seems to be too large. However, I would only need to generate a small subset of the 100! permutations, which for instance can be easily done with numpy:

np.random.permutation(np.arange(0,100))

Is there a similar workaround with KPermutations?
Thanks!

Hi,

Have you try to use the KPermutationsDistribution class? Something like this:

import openturns as ot

N = 50
M = 100
N_permutations = ot.KPermutationsDistribution(M, M).getSample(N)
permuted_values = [[int(x) for x in N_permutations[j]] for j in range(N)]
print(permuted_values)

The tricky part is the conversion between floats and integers, as in OT distributions produce floating point values.

Cheers

RĂ©gis

Hello,
I had missed the existence of the KPermutationsDistribution class. This work perfectly, thanks!
Best regards,
Julien