生成所有可能结果的矩阵,以掷出n个骰子(忽略顺序) [英] Generate a matrix of all possible outcomes for throwing n dice (ignoring order)

查看:106
本文介绍了生成所有可能结果的矩阵,以掷出n个骰子(忽略顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在顺序很重要的情况下,很容易生成所有可能结果的矩阵。一种实现方式是使用 expand.grid ,如

In cases where order does matter, it's rather easy to generate the matrix of all possible outcomes. One way for doing this is using expand.grid as shown here.

如果没有怎么办?

如果我是对的,可能的组合数是(S + N-1)!/ S!(N-1)! ,其中S是骰子的数量,每个骰子的N边编号为1到N。(与众所周知的组合公式不同,因为相同的数字可能出现在多个骰子上)。例如,投掷四个六面骰子时,N = 6和S = 4,因此可能的组合数为(4 + 6-1)!/ 4!(6-1)! = 9!/ 4!x5! =126。如何生成这126种可能结果的矩阵?

If I'm right, the number of possible combinations is (S+N-1)!/S!(N-1)!, where S is the number of dice, each with N sides numbered 1 through N. (It is different from the well known combinations formula because it is possible for the same number to appear on more than one dice). For example, when throwing four six-sided dice, N=6 and S=4, so the number of possible combinations is (4+6-1)!/4!(6-1)! = 9!/4!x5! = 126. How can I generate a matrix of these 126 possible outcomes?

谢谢。

推荐答案

以下是gd047和Marek足以提供的代码。

Here is code which gd047 and Marek were kind enough to provide.

S <- 6 
N <- 4 
n <- choose(S+N-1,N) 
outcomes <- t(combn(S+N-1,N,sort)) - matrix(rep(c(0:(N-1)),each=n),nrow=n)

注意:这是最佳的,因为它不会尝试生成所有对象,然后将其丢掉。 它实际上仅生成需要的那些

Note: this is optimal in the sense that it does not try to generate all and then throw away the dupes. It actually generates only those that are required.

其工作原理的解释:

骰子上可能的数字是1到N。

The possible numbers on the dice are 1 to N.

假设给定了骰子数字的可能组合:x 1 ,x 2 ,...,x S ,其中S是骰子数。

Suppose you are given a possible combination of the dice numbers: x1 , x2 , ..., xS where S is the number of dice.

由于顺序无关紧要,我们可以假设

Since the order does not matter, we can assume that

x 1 ≤ x 2 ≤ ...,≤ x S

x1 ≤ x2 ≤ ..., ≤ xS.

现在考虑序列x 1 ,x 2 + 1 ,x 3 + 2,...,x S + S-1。

Now consider the sequence x1, x2 + 1, x3 + 2, ..., xS + S-1.

(例如:1 ,1,1变成1,1 + 1,1 + 2 = 1,2,3)。

(Eg: 1,1,1 becomes 1,1+1,1+2 = 1,2,3).

此新序列的编号从1到N + S-1所有数字都是不同的。

This new sequence has numbers from 1 to N+S-1 and all numbers are distinct.

从骰子序列到我们创建的新骰子序列的映射为1-1,并且易于逆转。

This mapping from your dice sequence to new one we created is 1-1 and easily reversible.

因此要生成数字1到N的S骰子的可能组合,您需要做的就是生成所有N + S-1。从1,2,...,N + S-1中选择S个数字的S组合。给定这样一个组合,您可以对其进行排序,从最小的骰子中减去0,从第二个最小的骰子中减去1,依此类推,以获得S骰子的骰子数字组合,编号为1到N。

Thus to generate a possible combination of S dice with numbers 1 to N, all you need to do is to generate all N+S-1 Choose S combinations of S numbers from 1, 2, ..., N+S-1. Given such a combination, you sort it, subtract 0 from the smallest, 1 from the second smallest and so on to get your dice number combination for S dice numbered 1 to N.

例如,假设N = 6且S = 3。

For instance, say N = 6 and S = 3.

您生成3个数字的组合,从1到6 + 3-1 = 8,即3个数字从1,2,...,8开始。

You generate a combo of 3 numbers from 1 to 6+3-1 = 8, i.e 3 numbers from 1,2,...,8.

说您得到3,6,7。换算为3,6-1,7-2 = 3,5,5。

Say you get 3,6,7. This translates to 3, 6-1, 7-2 = 3,5,5.

如果您有1,2,8。这将转换为1,1,6。

If you got 1,2,8. This would translate to 1,1,6.

顺便说一句,此映射还证明了您的公式。

Incidentally, this mapping also proves the formula you have.

这篇关于生成所有可能结果的矩阵,以掷出n个骰子(忽略顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆