0
$\begingroup$

Given a list, possibly with repeated elements, I am trying to get a list that consists of a specified number of distinct permutations of the list. Essentially, I want to something that works like Take[Permutations[mylist],100] which gives the first 100 permutations of mylist. The issue is that this code first computes Permutations[mylist] and then takes the first 100 elements, which is a waste of time and memory space, and will throw errors if mylist is too large.

Essentially, I want something that gives the same output as Take[Permutations[mylist],100] but runs more efficiently.

This seems like a very basic problem but I can't figure out how to implement it. Any assistance is appreciated!

$\endgroup$
2
  • $\begingroup$ This may be useful, why-is-nextpermutation-slow $\endgroup$
    – chyanog
    Commented Sep 13, 2021 at 3:51
  • $\begingroup$ An example of a list with repeated elements would be handy to compare the timing of the solutions you'll get. $\endgroup$
    – JimB
    Commented Sep 13, 2021 at 4:21

1 Answer 1

2
$\begingroup$

I don't understand why you'd want anything but a random sample from the list of possible distinct permutations so I don't see that NextPermutation or UnrankPermutation in the Combinatorica package would be useful to generate 100 permutations in lexicographic or some other "fixed" order.

If the length of mylist is more than 10 and is full of distinct elements, then just taking a RandomSample 100 times is probably all you need. If there is some chance of duplicates, you could over-sample (i.e., more than 100 times) and just delete the duplicates.

mylist = Range[10];
x = (Table[RandomSample[mylist, Length[mylist]], {i, 2*100}] // DeleteDuplicates)[[1 ;; 100]];
$\endgroup$
1
  • $\begingroup$ This seems to work, thanks for the answer! $\endgroup$
    – YiFan
    Commented Sep 13, 2021 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.