matplotlib 如何使用变量和滚动方法比较骰子滚动的结果

ugmeyewa  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(130)

如何编码:
参与人1总是掷骰子A,参与人B总是掷骰子B,比较结果5782次,也分别是A对C,B对C。
似乎我无法打印出骰子A更大,骰子B更大的次数,然后,骰子A和B具有相等的值。
问题是,显然,die_A_larger,die_B_larger和die_A_and_B_equal是未定义的。我想知道为什么以及如何解决这个问题。
到目前为止的代码:

import numpy as np

class die:
    def __init__(self, side_values=[1,2,3,4,5,6]):
        self.side_values = side_values

    def roll(self):
        i=np.random.randint(1,7)
        return self.sides[i]

#variables
A = die([3,3,3,3,3,6])
B = die([2,2,2,5,5,5])
C = die([1,4,4,4,4,4])
nmax = 5782

def count_number_of_wins(A, B):

    die_A_larger = 0
    die_B_larger = 0
    die_A_and_B_equal = 0

for n in range(nmax):
    if A > B:
        die_A_larger += 1
    elif A < B:
        die_B_larger +=1
    else:
        die_A_and_B_equal +=1
    
    return (die_A_larger, die_B_larger, die_A_and_B_equal)
print (count_number_of_wins(A, B))

def determine_best_die(dice):
    assert all(len(die) == 6 for die in dice)

    wins = [0] * len(dice)

    for i in range(len(dice)):
        for j in range(i+1, len(dice)):
            a, b = count_number_of_wins(dice[i], dice[j])
            if a > b:
                wins[i] = wins[i] + 1
            else:
                wins[j] = wins[j] + 1
    for i in wins:
        if (i == (len(dice)-1)):
            return wins.index(i)
    return -1

字符串

mmvthczy

mmvthczy1#

您需要通过itertools对您在问题中描述的组合进行矢量化和重定向:

import itertools
from typing import Sequence

import numpy as np

class Die:
    def __init__(self, side_values: Sequence[int] = range(1, 7)) -> None:
        self.sides, counts = np.unique(side_values, return_counts=True)
        self.probabilities = counts / counts.sum()

    def roll(self, rand: np.random.Generator, n: int) -> np.ndarray:
        return rand.choice(a=self.sides, p=self.probabilities, size=n)

def main() -> None:
    rand = np.random.default_rng(seed=0)
    a = Die((3, 3, 3, 3, 3, 6))
    b = Die((2, 2, 2, 5, 5, 5))
    c = Die((1, 4, 4, 4, 4, 4))
    nmax = 5_782
    names = 'abc'

    for (name_1, die_1), (name_2, die_2) in itertools.combinations(
        zip(names, (a, b, c)), r=2,
    ):
        rolls_1 = die_1.roll(rand, n=nmax)
        rolls_2 = die_2.roll(rand, n=nmax)
        print(f'{name_1} < {name_2}: {np.count_nonzero(rolls_1 < rolls_2)}')
        print(f'{name_1} = {name_2}: {np.count_nonzero(rolls_1 == rolls_2)}')
        print(f'{name_1} > {name_2}: {np.count_nonzero(rolls_1 > rolls_2)}')

if __name__ == '__main__':
    main()

个字符

相关问题