Working Out Combinations For 9 Pegs Of 9 Possible Colours ( Mastermind Game ) 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.
 A: Coming up with a completely generic formula that covers all cases is frustratingly difficult or tedious to write... but if you have a specific what I'll call a "frequency-sequence" you are interested in, the approach is to choose which colors are used for which frequency where order of colors within the same frequency doesn't matter... then use multinomial coefficients to choose how to distribute those colors with those frequencies among the peg positions.
For example with $n=k=9$ and the frequency-sequence "3-2-2-1-1", begin by picking a color to appear three times.  This can be done in $9$ ways.  Next, simultaneously pick two colors from those remaining to appear two times each.  This can be done in $\binom{8}{2}$ ways.  Finally, simultaneously pick two colors to be used once each.  This can be done in $\binom{6}{2}$ ways.  Finally, pick the order in which those selected colors appear that many times in $\binom{9}{3,2,2,1,1}=\frac{9!}{3!2!2!1!1!}$ ways.
The overall count of this case then?  $9\cdot \binom{8}{2}\cdot \binom{6}{2}\cdot \frac{9!}{3!2!2!1!1!}$

As for a completely general formula... given a degree sequence in the form $((a_1,a_2,\dots,a_m),(b_1,b_2,\dots,b_m))$ with $b_i\neq b_j$ for all $i\neq j$ and $a_1b_1+a_2b_2+\dots+a_mb_m=n$ representing $a_1$ copies of $b_1$, $a_2$ copies of $b_2$ and so on... the earlier example being $((1,2,2),(3,2,1))$ corresponding to one $3$, two $2$'s, and two $1$'s...
we can express this as:
$\left(\prod\limits_{i=1}^m\binom{k-\sum\limits_{j=1}^{i-1}a_i}{a_i}\right)\cdot\frac{n!}{\prod\limits_{i=1}^m(b_i!)^{a_i}}$
