我需要使用heads = 0
和tails = 1
来模拟硬币的翻转。每次在1和0之间生成一个随机数时,需要在数组中增加和更新磁头或尾部。以下是我的代码:
import numpy, random
flips = numpy.array([0,0])
coin = heads = tails = 0
for i in range(10):
coin = random.randint(0,1)
if coin == 0:
heads += 1
(Now at this point, I want to update the second position of the array because that represents heads, how would I do that? And the same for the first position, with tails).
请帮助:)
答案 0 :(得分:0)
你可以使用pop
array = [1,2,3,4]
array.pop(1)
默认情况下,不带任何参数的pop会删除最后一项
array = [1,2,3,4] array.pop()
答案 1 :(得分:0)
不会有这个伎俩吗?
import numpy, random
flips = numpy.array([0,0])
for i in range(10):
flips[random.randint(0,1)] += 1