Python 3.x Python:如何模拟掷骰子100次
我想写一个程序来模拟100次骰子的滚动,但是我该怎么做呢? 代码如下:Python 3.x Python:如何模拟掷骰子100次,python-3.x,Python 3.x,我想写一个程序来模拟100次骰子的滚动,但是我该怎么做呢? 代码如下: import random def roll() : print('The computer will now simulate the roll of a dice 100 times') number = random.randint(1,6) print([number]) roll() 创建100卷的列表: import random print([random.randint(1,6) for _ in
import random
def roll() :
print('The computer will now simulate the roll of a dice 100 times')
number = random.randint(1,6)
print([number])
roll()
创建100卷的列表:
import random
print([random.randint(1,6) for _ in xrange(100)])
dansalmo的答案似乎不错,但如果你使用的是Numpy
您只需要使用
numpy.random.randint(1,6100)
,因为这样效率会更高,并且还将使用离散均匀分布取出值正确缩进代码并调用roll()
一百次,类似于for i in xrange(100):roll()
您希望得到什么样的输出?100卷的清单?