有没有相当于 pythons 的 c# itertools.combinations_with_replacement

is there a c# equivalent of pythons itertools.combinations_with_replacement

我正在寻找掷 5 个六面骰子的所有可能方法。我知道在 python 中你可以使用 itertools 来做到这一点,在 c# 中有什么东西可以达到同样的目的吗?

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

范围 1, 6 是骰子的面数, , 5 是掷骰子的数量。想要产生所有 7776 种掷骰子的方式。例如初始滚动可能如下所示:

骰子 1,骰子 2,骰子 3,骰子 4,骰子 5 = 1,2,3,4,5

3615 建议的实现:

Random root = new Random();
List<int> results = new List<int>()
for (int i = 0; i < 5; i++)
{
    results.Add(root.Next(1, 6));
}

//results now contain the 5 dice throws

这很简单 - 它基本上是 cross join 范围在 1-6 5 次之间。

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 };

实例:http://rextester.com/VKA17045