On 12/09/16 02:09, James A. Donald wrote:
I need to be able to do two of the following three tasks.
Generate a permutation of eighteen ones and eighteen zeros with equal
probability for each permutation. Or equivalently shuffle eighteen
black cards and eighteen red cards.
Sequentially generate all possible permutations with each permutation
generated once and only once.
Map between permutations and scalars, such that each permutation maps to
unique number, and the set of numbers that represents valid permutations
Could someone point me to the relevant literature, or literature for
converting between different representations of a permutation?
Since there are only two classes of items being shuffled, this class of
permutations has a variety of special and convenient properties.
I had a similar problem, the way I solved it was to create tables as
follows - I had to do a lot of conversions.
We are going to write the permutations as binary numbers, and then
number them according to size.
Look for the first 1 in the permutation, ie the 1 which is in the
It can be at position 18, and then all the lower bits will be ones so it
is permutation number 1.
If the first bit is in position 19 then there are 18 possible
lower-value permutations. These are permutations 2 to 19. The table
value for a first bit in position 19 is 2.
If the first bit is in position 20 then there are x possible
permutations, permutations 20 to x. The table value for a first bit in
position 20 is 20.
And so on, but we have to calculate x first.
Looking at permutations with a first bit in position 19, if the second
bit is in position 18 then it is worth 1, if it is in position 17 then
it is worth 0. If the third bit is in position 17, it is worth 1, and so on.
Note that eg a fifth bit in position 14, or a 17th bit in position 3, is
always worth 1, no matter where the other bits are positioned.
This is true (with the appropriate value) for any combination of
bitnumber and bitposition - it doesn't change if the other bits change
So we create a table with values for the first, second, third etc bits
of the permutation, and the positions in the permutation they are found
in. We total those values, which gives the number of the permutation.
Calculating the permutation from the permutation number is
straightforward, find possible ranges and subtract.
Hope this is clear, written in a rush. My email is real, you can contact
-- Peter Fairbrother