1

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).

请帮忙 :)

4

2 回答 2

0

你可以使用流行音乐

数组 = [1,2,3,4]

数组.pop(1)

现在数组是 [1,3,4]

默认情况下,不带任何参数的 pop 会删除最后一项

数组 = [1,2,3,4] 数组.pop()

现在数组是 [1,2,3]

于 2016-03-10T20:48:47.267 回答
0

这不是诀窍吗?

import numpy, random

flips = numpy.array([0,0])

for i in range(10):
    flips[random.randint(0,1)] += 1
于 2016-03-10T20:51:17.637 回答