[英]Is there a reason why I am getting the same input over and over even though I am using np.random.choice()?
I am given that someone playing Dungeons and Dragons (D&D) rolls a fair die with values 1-20 (one value per side) and that depending on the character I guess there is a modifier for each action that the character can make.我知道有人玩龙与地下城 (D&D) 时会掷出一个 1-20 的公平骰子(每边一个值),并且根据角色,我猜角色可以做出的每个动作都有一个修饰符。 In this case the modifier is 11 for opening a door.
在这种情况下,打开门的修饰符是 11。 If the dice roll + modifier is greater than 15, the action is successful and the character opens the door if not, the action fails.
如果掷骰 + 修正值大于 15,则动作成功,角色打开门,否则动作失败。 This part of the question wants us to make an array of seven dice rolls and the score for that character with the modifier of 11.
这部分问题希望我们制作一个包含 7 个骰子的数组,以及该角色的分数,修饰符为 11。
This is what I have tried so far这是我迄今为止尝试过的
import numpy as np
from datascience import *
modifier = 11
possible_rolls = np.arange(20)
roll_result = np.random.choice(possible_rolls)
modified_result = roll_result + modifier
num_observations = 7
def simulate_observations():
"""Produces an array of 7 simulated modified die rolls"""
possible_rolls = np.arange(20)
roll_result = np.random.choice(possible_rolls)
modified_result = roll_result + modifier
array = make_array()
for i in np.arange(num_observations):
array = np.append(array, modified_result)
return array
observation_array = simulate_observations()
print(observation_array)
I'm expecting to get a various range of outputs based on a random roll of the dice and then adding that value to the modifier, then finally placing that final value in to the array named array
but all I am getting is an array that looks like [20.,20.,20.,20.,20.,20.,20.]
.我期望根据骰子的随机滚动获得各种输出范围,然后将该值添加到修饰符,然后最终将最终值放入名为
array
的数组中,但我得到的只是一个看起来像[20.,20.,20.,20.,20.,20.,20.]
。 Any ideas as to where I may be off?关于我可能会去哪里的任何想法? I'm 90% sure my issue is in my for loop as I have not quite grasped what exactly I am doing in them but I can't seem to pin down exactly what the problem is.
我 90% 确定我的问题出在我的 for 循环中,因为我还没有完全掌握我在它们中到底在做什么,但我似乎无法确定到底是什么问题。
It is because you call np.random.choice
only once, instead of once in each iteration in the for loop这是因为您只调用
np.random.choice
一次,而不是在 for 循环中的每次迭代中调用一次
import numpy as np
from datascience import *
modifier = 11
possible_rolls = np.arange(20)
roll_result = np.random.choice(possible_rolls)
modified_result = roll_result + modifier
num_observations = 7
def simulate_observations():
"""Produces an array of 7 simulated modified die rolls"""
possible_rolls = np.arange(20)
array = make_array()
for i in np.arange(num_observations):
# moved roll_result inside the loop
roll_result = np.random.choice(possible_rolls)
modified_result = roll_result + modifier
array = np.append(array, modified_result)
return array
observation_array = simulate_observations()
print(observation_array)
# [20. 11. 24. 11. 15. 16. 26.]
But you can also avoid the for loop and do directly但是你也可以避免for循环,直接做
def simulate_observations():
"""Produces an array of 7 simulated modified die rolls"""
possible_rolls = np.arange(modifier,20+modifier)
return np.random.choice(possible_rolls,size=num_observations).tolist()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.