骰子概率问题 [英] Dice probability problem

查看:50
本文介绍了骰子概率问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正试图找到一种方法来计算任意组合骰子的
结果分布。我已经完成了基本工作

,但我有点不确定如何继续。我的主要关注点

是如何让这个接受任意数量的骰子,而不必为每个案例编写新的列表理解?


这是一段代码,显示我正在做的事情

目前。


- 代码开始 - -


#带有n个面孔的模具

D = lambda n:[x + 1表示范围内的x(n)]


#3个骰子池,每个6个面孔

池= [D(6)] * 3


#A列表使用当前3d6池的所有结果。

results = [x + y + z表示池中的x [0]表示池中的y [1]

表示池中的z [2]]


#持有发行版的字典

发行= {}


#如果结果已经是一个关键,它的价值增加了​​1。

#否则为键添加结果并设置其值

#为1.

def count(x):

如果distribution.has_key(x):distribution [x] + = 1

else:distribution [x] = 1

#使用以上计数功能映射结果。

地图(计数,结果)


- 代码结束 -


谢谢,

Tomi Lindberg

解决方案

Tomi Lindberg< to ** *****************@pp.inet.fi.invalid>写道:

我正试图找到一种方法来计算任何骰子组合的结果分布。我已经完成了基础知识,但是我有点不确定如何继续。我主要担心的是如何让这个接受任意数量的骰子,而不必为每个案例写一个新的列表理解?




你需要思考关于将问题分解为一些

操作的正确方法,可以重复次数比骰子少一次(如果你只需要一个骰子,只需要一个骰子,没有什么需要待完成)然后重复它。


一个显而易见的候选人是在目前计算的总和上添加一个骰子:


def addDice (总和,骰子):

返回[x + y代表骰子中的x为总和]


如果你的骰子少于1个,答案是

#len(游泳池)== 1

游泳池[0]


之后,每次添加骰子你需要为所有以前的骰子和新骰子计算的总和

调用addDice:


#len(pool)== 2

addDice(resultFor1,pool [1])

addDice(pool [0],pool [1])


then
<无线电通信/>
#len(pool)== 3

addDice(resultFor2,pool [2])

addDice(addDice(resultFor1,pool [1]) ,游泳池[2])

addDice(addDice(pool [0],pool [1]),pool [2])


最后你得到


#len(池)== n

addDice(addDice(addDice(...,pool [n-3]),pool [n-2 ])游泳池[n-1])


好​​的,那么我们如何获得重复?


方便的模式f(.. .f(f(x [0],x [1]),x [2])...,x [n-1])或等价,

如果我们写中缀运算符*对于f:x [0] * x [1] * ... * x [n-1],可以在python中写成

reduce(f,x)。所以我们得到:


reduce(addDice,pool)

== reduce(lambda sums,dice:[x + y for x in dice in y in in总和],池)


你应该尝试将其作为单个函数编写,不使用reduce(但是识别(左)减少模式使用
是有用的,即使你没有使用python'的减少)。


''


Alexander Schmolck< a。******** @ gmail.com>写道:

addDice(resultFor1,pool [1])
addDice(pool [0],pool [1])



抱歉应该说明连续的行是

等价物,即


addDice(resultFor1,pool [1])

== addDice(pool [0],pool [1])


''


Op 2006- 04-04,Tomi Lindberg schreef< to ******************* @ pp.inet.fi.invalid>:



我正试图找到一种方法来计算任意骰子组合的结果分布。我已经完成了基础知识,但我有点不确定如何继续。我主要关心的是如何让这个接受任意数量的骰子,而不必为每个案例写一个新的列表理解?


国际海事组织你从错误的一面看它。


最好为一个构建分配

死掉并制作一个可以添加两个发行版的功能

。因此,对于3D6,您首先将分配给B6的D6分配给这个结果

再次添加D6的分布。


如果你需要更多的东西开始,请问。

这里有一段代码,显示了我现在正在做的事情。

- 代码开始 -
#一个有n个面的模具
D = lambda n:[x + 1表示x在范围内(n)]

#一个3个骰子池,每个骰子有6个面孔
池= [D(6)] * 3

#A当前3d6池的所有结果列表。
结果= [x + y + z表示池中的x [0]表示池中的y [1]
表示池中的z [2]]



这是非常低效的。我不想用这种方式来计算10D10的分布。


试着想想如何只用D2'来做这件事。


(帕斯卡的三角形)并概括它。


-

Antoon Pardon


Hi,

I''m trying to find a way to calculate a distribution of
outcomes with any combination of dice. I have the basics
done, but I''m a bit unsure how to continue. My main concern
is how to make this accept any number of dice, without
having to write a new list comprehension for each case?

Here''s a piece of code that shows the way I''m doing things
at the moment.

-- code begins --

# A die with n faces
D = lambda n: [x+1 for x in range(n)]

# A pool of 3 dice with 6 faces each
pool = [D(6)] * 3

# A List of all outcomes with the current 3d6 pool.
results = [x+y+z for x in pool[0] for y in pool[1]
for z in pool[2]]

# A dictionary to hold the distribution
distribution = {}

# If outcome is already a key, adds 1 to its value.
# Otherwise adds outcome to keys and sets its value
# to 1.
def count(x):
if distribution.has_key(x): distribution[x] += 1
else: distribution[x] = 1

# Maps the results with above count function.
map(count, results)

-- code ends --

Thanks,
Tomi Lindberg

解决方案

Tomi Lindberg <to*******************@pp.inet.fi.invalid> writes:

I''m trying to find a way to calculate a distribution of outcomes with any
combination of dice. I have the basics done, but I''m a bit unsure how to
continue. My main concern is how to make this accept any number of dice,
without having to write a new list comprehension for each case?



You need to think about the right way to break the problem down into some
operation that can be repeated one fewer times than there are dice (if you
just have a single dice, nothing needs to be done) and then repeat it.

An obvious candidate is adding a single dice to the sums computed so far:

def addDice(sums, dice):
return [x+y for x in dice for y in sums]

If you have less than 1 dice the answer is
# len(pool) == 1
pool[0]

After that, each time you add a dice you need to call addDice on the sum
computed for all the previous dice and the new dice:

# len(pool) == 2
addDice(resultFor1, pool[1])
addDice(pool[0], pool[1])

then

# len(pool) == 3
addDice(resultFor2, pool[2])
addDice(addDice(resultFor1, pool[1]), pool[2])
addDice(addDice(pool[0], pool[1]), pool[2])

finally you get

# len(pool) == n
addDice(addDice(addDice(..., pool[n-3]), pool[n-2]) pool[n-1])

OK, so how do we get the repetition?

Conveniently the pattern f(...f(f(x[0],x[1]),x[2])...,x[n-1]) or equivalently,
if we write the infix operator * for f: x[0]*x[1]*...*x[n-1], can just be written as
reduce(f, x) in python. So we get:

reduce(addDice, pool)
== reduce(lambda sums, dice: [x+y for x in dice for y in sums], pool)

You should presumably also try writing this out as a single function, without
using reduce (but recognizing the (left) reduction pattern is useful, even if
you don''t use python''s reduce).

''as


Alexander Schmolck <a.********@gmail.com> writes:

addDice(resultFor1, pool[1])
addDice(pool[0], pool[1])


sorry should have spelled out that successive lines are meant to be
equivalent, i.e.

addDice(resultFor1, pool[1])
== addDice(pool[0], pool[1])

''as


Op 2006-04-04, Tomi Lindberg schreef <to*******************@pp.inet.fi.invalid>:

Hi,

I''m trying to find a way to calculate a distribution of
outcomes with any combination of dice. I have the basics
done, but I''m a bit unsure how to continue. My main concern
is how to make this accept any number of dice, without
having to write a new list comprehension for each case?
IMO you are looking at it from the wrong side.

It would be better to construct distributions for one
die and make a function that can ''add'' two distributions
together. So for 3D6 you first add the distribution of
a D6 to the distribution of a D6 and to this result
you add the distribution of a D6 again.

If you need more to start, just ask.
Here''s a piece of code that shows the way I''m doing things
at the moment.

-- code begins --

# A die with n faces
D = lambda n: [x+1 for x in range(n)]

# A pool of 3 dice with 6 faces each
pool = [D(6)] * 3

# A List of all outcomes with the current 3d6 pool.
results = [x+y+z for x in pool[0] for y in pool[1]
for z in pool[2]]



This is very inefficient. I wouldn''t want to calculate
the distribution of 10D10 this way.

Try to think how you would do this with only D2''s.

(Triangle of Pascal) and generalize it.

--
Antoon Pardon


这篇关于骰子概率问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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