code: def generate_perm(k, nperm=100):
      """
      returns an array of shape(nbperm, k) representing
      the permutations of k elements
  
      Parameters
      ----------
      k, int the number of elements to be permuted
      nperm=100 the maximal number of permutations
      if gamma(k+1)>nperm: only nperm random draws are generated
  
      Returns
      -------
      p: array of shape(nperm,k): each row is permutation of k
      """
      from scipy.special import gamma
  
      if k == 1:
          return np.reshape(np.array([0]), (1, 1)).astype(np.int_)
      if gamma(k + 1) < nperm:
          # exhaustive permutations
          aux = generate_perm(k - 1)
          n = aux.shape[0]
          perm = np.zeros((n * k, k)).astype(np.int_)
          for i in range(k):
              perm[i * n : (i + 1) * n, :i] = aux[:, :i]
              perm[i * n : (i + 1) * n, i] = k - 1
              perm[i * n : (i + 1) * n, i + 1 :] = aux[:, i:]
      else:
          from numpy.random import rand
  
          perm = np.zeros((nperm, k)).astype(np.int_)
          for i in range(nperm):
              p = np.argsort(rand(k))
              perm[i] = p
      return perm
  
imports: import math
  import numpy
  import numpy.random
  from scipy.linalg import cholesky, eigvalsh, inv
  from scipy.special import gammaln
  from .gmm import GMM
  from .utils import kmeans
label_desc: Use image node_id 7 for calling a global function with example usage: generate_perm(k, nperm) and returns: perm, np