1

I need to simulate the flipping of a coin, with heads = 0 and tails = 1. And each time a random number is generated between 1 and 0, heads or tails needs to be incremented and updated in the array. Below is the code I have:

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

Please help :)

Matthias
  • 4,481
  • 12
  • 45
  • 84
Dini411
  • 15
  • 5

2 Answers2

0

you could use pop

array = [1,2,3,4]

array.pop(1)

now array is [1,3,4]

By default, pop without any arguments removes the last item

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

now array is [1,2,3]

Zain Hatim
  • 80
  • 6
  • So array.pop() for an array is basically the equivalent the remove or delete function for a list? – Dini411 Mar 13 '16 at 09:12
  • the method remove for the list you pass an the value to be deleted, for the pop method you pass an the index. – Zain Hatim Mar 14 '16 at 18:22
0

Wouldn't that do the trick ?

import numpy, random

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

for i in range(10):
    flips[random.randint(0,1)] += 1
Pholochtairze
  • 1,836
  • 1
  • 14
  • 18