I think that to save your result (which will be an ordered list of N elements) there will be O (N) in memory, no?
Anyway, to answer your later question about choosing a random permutation, here is a technique that would be better than just producing all N! features in the list, say, and then randomly select an index. If we can just select the index randomly and generate the permutation associated with it, we are much better.
We can represent the dictionary order in your words / permutations and associate a unique number with them based on the word order / permutations of the appearance in the dictionary. For example, words on three characters will be
perm. index 012 <----> 0 021 <----> 1 102 <----> 2 120 <----> 3 201 <----> 4 210 <----> 5
You will see later why itโs easiest to use the numbers we made, but others can be placed with a bit more work.
To select one at random, you can randomly select your linked index from the range 0 ... N! -1 with uniform probability (the simplest implementation of this obviously cannot be solved even for moderately large N, I know, but I think there are decent workarounds), and then determine the permutation associated with it. Please note that the list starts with all permutations of the last elements of N-1, while maintaining the first digit equal to 0. After these possibilities are exhausted, we generate all those that begin with 1. After these next (N-1) ! permutations are exhausted, we generate those that start with 2. Etc. Thus, we can determine that the leading digit is Floor [R / (N-1)!], Where R is the index in the above sense. See also why we indexed zero too?
To generate the remaining N-1 digits in the permutation, let's say that we defined Floor [R / (N-1)!] = A0. Start with the list {0, ..., N-1} - {a0} (calculated subtraction). We need the Qth permutation of this list for Q = R mod (N-1) !. Except for the fact that there is no number there, this is exactly the same as the problem we just solved. Recurse