【问题标题】:Dice Statistics骰子统计
【发布时间】:2021-12-26 05:29:06
【问题描述】:

我想用 Python 构建一个骰子统计模拟器。这个想法是你有一个起始数组,包含 [1,...,1] 用于最低可能的骰子掷骰,一个。我已经有一个 4d4 的例子,这很有效,所以我希望我的代码对每种组合都这样做。所以我写了变量和函数,供递归使用。但它给了我一个输出,这是不正确的而且太高了。也许有人可以帮助我,如果你需要解释,那么你可以问。

sides = 4
rolls = 4
constant = sides**rolls
beginningtable = []

for i in range(rolls):
    beginningtable.append(1)


values = {rolls : 0}
check = True

print("-------------")

def checking(valuetable):
    print(valuetable)
    sum = 0

    checker = 0
    for i in valuetable:
        sum = sum + i

    for i in values.keys():
        if sum == i:
            values[i] = values [i] + 1
            checker = 1

    if checker == 0:
        values[sum] = 1



def gothroughindex(table, index):

    checking(table)

    for i in range(sides-1):
        table[index] += 1
        checking(table)

    return table

def loop(looptable, loopcounter):
    for i in range(loopcounter):
        if i != (loopcounter-1):
            looptable[i] = 1
        else:
            looptable[i] += 1
    return looptable

def recursiveloop(table,counter, loopvalue):



    if counter == rolls:
        for i in range(rolls):
            print("\ncounter == counter\n")
            recursiveloop(table, counter-1, loopvalue-1)
            table = loop(table, loopvalue)
    elif counter == 1:
        for i in range(rolls-1):
            print("\ncounter == 1\n")
            print(loopvalue)
            gothroughindex(table, 0)
            table = loop(table, loopvalue+1)
    else:
        for i in range(rolls):
            print("\nelse\n")
            print(str(loopvalue) + " here I am")

            recursiveloop(table, counter-1, loopvalue-1)

            gothroughindex(table, 0)
            table = loop(table, loopvalue)



def mainthing(maintable):
    counter = rolls
    loopvalue = rolls
    recursiveloop(maintable, counter, loopvalue)



"""
    for i in range(rolls): #Das sind die ersten vier
        for i in range(rolls): #Das sind die ersten drei
            for i in range(rolls-1): #Das sind die ersten beiden
                gothroughindex(maintable, 0)
                maintable = loop(maintable, 2)    
            gothroughindex(maintable, 0)
            maintable = loop(maintable, 3)
        maintable = loop(maintable, 4)
    """

    print("---------")
    print(maintable)
    print(values)

    sum = 0
    for i in values.keys():
        sum = values[i] + sum
    print(sum)

mainthing(beginningtable)

【问题讨论】:

  • 我不明白你的问题....不是最低可能的滚动总是全1吗?
  • @JoranBeasley 我的算法想要计算每一个可能的掷骰子组合,你从只有一个组合开始,是的,例如2d3 --> [1,1] --> [2,1] --> [3,1] --> [1,2] --> [2,2] --> [3,2] - -> [1,3] --> [2,3] --> [3,3]

标签: python probability dice


【解决方案1】:

你能不能简单地把这一切简单化并使用 itertools 产品方法来生成这些?

为骰子的边数生成一个范围,然后将其用作要从中挑选的产品的迭代器。指定骰子的数量作为重复,以便产品将生成那么多项目

你可以在这里https://docs.python.org/3/library/itertools.html#itertools.product阅读更多关于itertools的信息

itertools.product(*iterables, 重复=1) 输入的笛卡尔积 可迭代。

要计算可迭代对象与自身的乘积,请指定数字 带有可选的重复关键字参数的重复次数。例如, product(A, repeat=4) 和 product(A, A, A, A) 意思一样。

from itertools import product


def roll_combos(num_dice, num_sides):
    return product(range(1, num_sides + 1), repeat=num_dice)


print(list(roll_combos(2, 3)))
print(list(roll_combos(4, 4)))

输出

[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 1, 3), (1, 1, 1, 4), (1, 1, 2, 1), (1, 1, 2, 2), (1, 1, 2, 3), (1, 1, 2, 4), (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), (1, 1, 3, 4), (1, 1, 4, 1), (1, 1, 4, 2), (1, 1, 4, 3), (1, 1, 4, 4), (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 1, 3), (1, 2, 1, 4), (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), (1, 2, 2, 4), (1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 2, 3, 4), (1, 2, 4, 1), (1, 2, 4, 2), (1, 2, 4, 3), (1, 2, 4, 4), (1, 3, 1, 1), (1, 3, 1, 2), (1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), (1, 3, 2, 4), (1, 3, 3, 1), (1, 3, 3, 2), (1, 3, 3, 3), (1, 3, 3, 4), (1, 3, 4, 1), (1, 3, 4, 2), (1, 3, 4, 3), (1, 3, 4, 4), (1, 4, 1, 1), (1, 4, 1, 2), (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 1), (1, 4, 2, 2), (1, 4, 2, 3), (1, 4, 2, 4), (1, 4, 3, 1), (1, 4, 3, 2), (1, 4, 3, 3), (1, 4, 3, 4), (1, 4, 4, 1), (1, 4, 4, 2), (1, 4, 4, 3), (1, 4, 4, 4), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), (2, 1, 1, 4), (2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), (2, 1, 2, 4), (2, 1, 3, 1), (2, 1, 3, 2), (2, 1, 3, 3), (2, 1, 3, 4), (2, 1, 4, 1), (2, 1, 4, 2), (2, 1, 4, 3), (2, 1, 4, 4), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), (2, 2, 1, 4), (2, 2, 2, 1), (2, 2, 2, 2), (2, 2, 2, 3), (2, 2, 2, 4), (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3), (2, 2, 3, 4), (2, 2, 4, 1), (2, 2, 4, 2), (2, 2, 4, 3), (2, 2, 4, 4), (2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 1), (2, 3, 2, 2), (2, 3, 2, 3), (2, 3, 2, 4), (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), (2, 3, 3, 4), (2, 3, 4, 1), (2, 3, 4, 2), (2, 3, 4, 3), (2, 3, 4, 4), (2, 4, 1, 1), (2, 4, 1, 2), (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 1), (2, 4, 2, 2), (2, 4, 2, 3), (2, 4, 2, 4), (2, 4, 3, 1), (2, 4, 3, 2), (2, 4, 3, 3), (2, 4, 3, 4), (2, 4, 4, 1), (2, 4, 4, 2), (2, 4, 4, 3), (2, 4, 4, 4), (3, 1, 1, 1), (3, 1, 1, 2), (3, 1, 1, 3), (3, 1, 1, 4), (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), (3, 1, 2, 4), (3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), (3, 1, 3, 4), (3, 1, 4, 1), (3, 1, 4, 2), (3, 1, 4, 3), (3, 1, 4, 4), (3, 2, 1, 1), (3, 2, 1, 2), (3, 2, 1, 3), (3, 2, 1, 4), (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), (3, 2, 2, 4), (3, 2, 3, 1), (3, 2, 3, 2), (3, 2, 3, 3), (3, 2, 3, 4), (3, 2, 4, 1), (3, 2, 4, 2), (3, 2, 4, 3), (3, 2, 4, 4), (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), (3, 3, 1, 4), (3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), (3, 3, 2, 4), (3, 3, 3, 1), (3, 3, 3, 2), (3, 3, 3, 3), (3, 3, 3, 4), (3, 3, 4, 1), (3, 3, 4, 2), (3, 3, 4, 3), (3, 3, 4, 4), (3, 4, 1, 1), (3, 4, 1, 2), (3, 4, 1, 3), (3, 4, 1, 4), (3, 4, 2, 1), (3, 4, 2, 2), (3, 4, 2, 3), (3, 4, 2, 4), (3, 4, 3, 1), (3, 4, 3, 2), (3, 4, 3, 3), (3, 4, 3, 4), (3, 4, 4, 1), (3, 4, 4, 2), (3, 4, 4, 3), (3, 4, 4, 4), (4, 1, 1, 1), (4, 1, 1, 2), (4, 1, 1, 3), (4, 1, 1, 4), (4, 1, 2, 1), (4, 1, 2, 2), (4, 1, 2, 3), (4, 1, 2, 4), (4, 1, 3, 1), (4, 1, 3, 2), (4, 1, 3, 3), (4, 1, 3, 4), (4, 1, 4, 1), (4, 1, 4, 2), (4, 1, 4, 3), (4, 1, 4, 4), (4, 2, 1, 1), (4, 2, 1, 2), (4, 2, 1, 3), (4, 2, 1, 4), (4, 2, 2, 1), (4, 2, 2, 2), (4, 2, 2, 3), (4, 2, 2, 4), (4, 2, 3, 1), (4, 2, 3, 2), (4, 2, 3, 3), (4, 2, 3, 4), (4, 2, 4, 1), (4, 2, 4, 2), (4, 2, 4, 3), (4, 2, 4, 4), (4, 3, 1, 1), (4, 3, 1, 2), (4, 3, 1, 3), (4, 3, 1, 4), (4, 3, 2, 1), (4, 3, 2, 2), (4, 3, 2, 3), (4, 3, 2, 4), (4, 3, 3, 1), (4, 3, 3, 2), (4, 3, 3, 3), (4, 3, 3, 4), (4, 3, 4, 1), (4, 3, 4, 2), (4, 3, 4, 3), (4, 3, 4, 4), (4, 4, 1, 1), (4, 4, 1, 2), (4, 4, 1, 3), (4, 4, 1, 4), (4, 4, 2, 1), (4, 4, 2, 2), (4, 4, 2, 3), (4, 4, 2, 4), (4, 4, 3, 1), (4, 4, 3, 2), (4, 4, 3, 3), (4, 4, 3, 4), (4, 4, 4, 1), (4, 4, 4, 2), (4, 4, 4, 3), (4, 4, 4, 4)]

【讨论】:

  • 哦,我不知道,python中有这样的围兜......非常感谢。
  • 但是您能否澄清一下您的代码是如何工作的?
  • 您可以从这里的 itertools docs.python.org/3/library/itertools.html#itertools.product 阅读有关产品方法的所有信息。我会用一个简短的解释来更新答案
  • 是的,我已经通读过了,抱歉我不够清楚。例如 4d4 骰子。第一:如果我理解正确,您在 product(range(1,...)) 处定义一个元组,范围为 1,5,但这对我来说没有任何意义,因为 4 面骰子不能有数字5。第二:我没有真正理解重复变量的含义。我对其进行了一些测试,如果我采用 repeat = 2,我会得到带有 [1,1] [1,2] 等的元组,并且 repeat = 3 [1,1,1] [1,1,2] 所以这个重复变量描述了我的元组中有多少个总数?
  • 您也可以从文档中查找范围函数。 range 将为您生成从起始编号到结束编号​​的数字列表,但不包括结束编号。因此 range(1, 5) 将生成数字 1、2、3、4。因为 5 是要停止的数字(即不包括在内)。现在以 2d4 为例。我们使用 4 来生成 d4 可能拥有的数字,然后我们使用 repeat 表示我们有 2 个骰子,因此我们每次需要选择 2 个数字。
猜你喜欢
  • 2014-08-25
  • 2015-07-18
  • 2021-08-19
  • 2021-03-23
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
相关资源
最近更新 更多