I have written a Mastermind clone where there are 9 peg-slots where each of the peg-slots can be 1 of 9 colours. I cannot seem to get my head round working out the number of permutations for particular repeated counts of any colours :( I am wanting to create a table of the frequencies of possible solution compositions ( or at least the most common ones / easiest to calculate ) , e.g. solutions with 9 separate colours, solutions with 1 and only 1 duplicated colour ( e.g. 9 pegs with 2 blue pegs and the rest all different OR 2 red pegs and the rest all different colours, etc ) , solutions with 2 and only 2 duplicated colours ( e.g. 9 pegs with 2 blue pegs AND 2 red pegs the rest all different OR 2 red pegs AND 2 orange pegs AND the rest all different colours, etc ), solutions with 1 peg appearing 3 times and the rest all different, solutions with 1 peg appearing 3 times, 1 appearing 2 times AND the rest all different-coloured pegs.
The total number of possible combinations is 99 which approximates to just over 184 million as any peg-slot can contain any colour. ( i assume i am right here? :) i.e. any peg colour can go in any slot so 9x9x9x9x9... )
The number of possible permutations with each peg being a different colour is 9! which is 362,880 which translates as the chance of getting a randomly generated solution with 9 separate colours as 362,880/184 million = 0.00094. That is that one would expect approx 1 in a 1000 to have distinct colours.
Now, to calculate the number of permutations with 1 and only 1 repeated colour. So this is the same as taking the permutations for 9 separate colours and changing one of the colours, say the yellow, to any other, say the blue. In doing this, we will have 2 repeated permutations: one where the yellow in Position x was changed to a blue, and the original blue in Position y, and another permutation where Yellow in Position y was changed to a blue, and the peg in Position x was a blue. So we can process the solution the same by substituting the yellow with another of the other 7 colours other than yellow ( all colours EXCEPT blue or yellow )to find all the possibilities with blue repeated. And we can do this for every colour.
The number of solutions where a particular colour is replaced by another particular colour = 362,880 /2.
The number of solutions where a particular colour being replaced by any other colour = (362,880 /2) * 8.
The number of solutions where any colour being replaced by any other colour = (362,880 /2) * 8 * 9.
Which means that the chances of a repeated colour is 36 times more likely than having a randomly-generated solution with ALL the colours represented.
Is my reasoning correct?
The problem is I have difficulty in visualising it to have solutions with 2 different colours being replaced by 2 other separate colours :( or extending it to substitute 2 colours with another so a colour, and only one colour, is tripled.
Thanks in advance.
And I am sure there is an easy formula to do this.