danny - 2 months ago 7x

C# Question

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)

https://docs.python.org/dev/library/itertools.html#itertools.combinations_with_replacement

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

Answer

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

Source (Stackoverflow)

Comments