【问题标题】:How could Improve these simple dice rolling functions to produce histogram plots?如何改进这些简单的掷骰子函数来生成直方图?
【发布时间】:2020-05-09 16:45:38
【问题描述】:

更快的编码方式?

我想知道是否有人可以建议一种更快的编码方式?我的任务是为 2-3 个骰子制作骰子滚动函数,并在直方图中显示结果。我知道 while/for 循环不是 python 中的禁食。谁能建议一个更快的内置函数来实现这一点?

import numpy as np
import matplotlib.pyplot as plt
#Write a function that will return a random int between 1-6
def roll_die():
    x = np.random.random_integers(6)
    return x
#A function to calculate the sum of 2 dice using a while loop
def rolling2(x):
    n=0
    while(n < rolls):
        sum = roll_die() + roll_die()
        arr[n] = sum
        sum = 0
        n = n + 1

#A function to calculate the sum of 3 dice using a while loop
def rolling3(x):
    n=0
    while(n < rolls):
        sum = roll_die() + roll_die() + roll_die()
        arr[n] = sum
        sum = 0
        n = n + 1

rolls = int (input("How many times will you roll 2 dice?"))
#Initialise 2 arrays to store values
arr = np.zeros(rolls)
arr1 = np.zeros(rolls)
#Calling rolling functions
rolling2(arr)
print (arr)

rolling3(arr1)
print(arr1)
#Plot graph using matplotlib methods and functions
fig, ax = plt.subplots(2,1)
ax[0].hist(arr, bins=11)
ax[0].set_xlabel("Sum of 2 Dice")
ax[0].set_label("Frequency")
ax[0].set_title("Frequency of the Sum of Rolling 2 dice")

ax[1].hist(arr, bins=16)
ax[1].set_xlabel("Sum of 3 Dice")
ax[1].set_label("Frequency")
ax[1].set_title("Frequency of the Sum of Rolling 3 dice")
plt.show()

【问题讨论】:

标签: python-3.x numpy matplotlib


【解决方案1】:

您可以使用列表推导式并在其中直接对两个或三个骰子进行求和:

我还删除了您对初始化数组的使用,因为它不是必需的。最好使用return 值而不是修改数组。

import numpy as np
import matplotlib.pyplot as plt

# A function that will return a random int between 1-6.
def roll_die():
    return np.random.random_integers(6)

# A function to calculate the sum of 2 dice rolls.
def rolling2(rolls):
    return [(roll_die() + roll_die()) for n in range(rolls)]

# A function to calculate the sum of 3 dice rolls.
def rolling3(rolls):
    return [(roll_die() + roll_die() + roll_die()) for n in range(rolls)]

rolls = int(input("How many times will you roll 2 dice?"))
array2 = rolling2(rolls)
array3 = rolling3(rolls)
print(array2)
print(array3)

# Plot graph using matplotlib methods and functions.
fig, ax = plt.subplots(2,1)
ax[0].hist(array2, bins=11)
ax[0].set_xlabel("Sum of 2 dices")
ax[0].set_label("Frequency")
ax[0].set_title("Frequency of the Sum of Rolling 2 dice")

ax[1].hist(array3, bins=16)
ax[1].set_xlabel("Sum of 3 dices")
ax[1].set_label("Frequency")
ax[1].set_title("Frequency of the Sum of Rolling 3 dice")
plt.show()

编写一个函数来滚动任意数量的骰子任意次数也很好。就是这样:

def roll_dices(dices, rolls):
    return [sum(roll_die() for _ in range(dices)) for _ in range(rolls)]

那么你可以打电话给roll_dices(2, rolls)而不是rolling2(rolls)

【讨论】:

  • 了解该死的爱情清单!这使它变得无限容易。嵌套的 for 循环也很有意义。 1个选择骰子,另一个滚动n次。
  • @KishanBhatt 确实!这是一种快速生成列表的非常方便的方法。在这里你没有太多的元素,但列表推导总是比 for 循环和追加更快。如果列表的元素太复杂,它们的可读性就会降低。
猜你喜欢
  • 2015-04-30
  • 2021-08-19
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多