# How to generate all the combinations with repetition?

I have $$K$$ variables.

Each variable can take any value form a set with $$N$$ elements.

We have $$N^K$$ possible solutions (permutations with repetition, when at each time slot we can choose among $$N$$ elements each time). However, some of these $$N^K$$ possible solutions will provide the same offered rate (we do not care about the ordering). So, the possible solutions reduce to:

$$\frac{(K+N-1)!}{K!(N-1)!}$$

How can I generate all these possible combinations when $$N=7$$, $$K=20$$?

• Can you please illustrate the desired combinations with a few small examples? – Kiro May 3 at 9:30
• @Kiro, please see my edit. – dipak narayanan May 3 at 9:38

With[{n = 2, k = 3},
Join @@ Table[IntegerPartitions[s, {k}, Range[n]], {s, k, n k}]]


{{1, 1, 1}, {2, 1, 1}, {2, 2, 1}, {2, 2, 2}}

With[{n = 7, k = 20},
Join @@ Table[IntegerPartitions[s, {k}, Range[n]], {s, k, n k}]]


{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, ..., {7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6}, {7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}

(230230 solutions)