danny danny - 10 months ago 54
C# Question

is there a c# equivalent of pythons itertools.combinations_with_replacement

I'm looking to find all the possible ways of rolling 5 six-sided dice. I know in python you can do this using itertools, is there something in c# that can achieve the same?

itertools.combinations_with_replacement(iterable, r)

for i in itertools.combinations_with_replacement(range(1, 6), 5)


The range 1, 6 is number of face on the dice and , 5 is the number of dice being rolled. Want to produce all 7776 ways you can roll the dice. e.g. an initial roll may look like:

Dice 1, Dice 2, Dice 3, Dice 4, Dice 5 = 1,2,3,4,5


This is easy - its basically a cross join between a range of 1-6 5 times.

var range = Enumerable.Range(1,6);
var result = from d1 in range
                     from d2 in range
                     from d3 in range
                     from d4 in range
                     from d5 in range
            select new { d1,d2,d3,d4,d5 };

Live example: http://rextester.com/VKA17045