'Calculate all possible combinations of traits, while taking into account trait rarity/limits

I'm working on a unique NFT generator and I need a mathematical equation to help me calculate how many combinations I can create from an X amount of layers of traits/properties.

For example, say I have 4 layers, with 5 traits in each layer. The expression for this is simple, its 5 x 5 x 5 x 5 = 625 possible combinations.

Now, what if I want one of the traits in one of the layers to only be used in, for example, lets say, 10 combinations.

What would be the expression to calculate the possible combinations of something like that, while taking these limits for specific traits into account?



Solution 1:[1]

Separate the case into combinations with that trait:

1 x 5 x 5 x 5 = 125

and combinations without that trait:

4 x 5 x 5 x 5 = 500

Limit the combinations with that trait to 10, and you get:

10 + 500 = 510

If you have multiple restricted traits, it makes sense to combine them, so as to get the greatest possible use of the unrestricted traits.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Beta