Python: Write a bit of code that will simulate 100 random, independent coin flips. Write code that will count the number of changes (Heads to Tails or Tails to Heads. For example, the sequence H, H, T, T, H, H, T has three changes.) in a sequence of coin flips. Hint: You can change the sequence to True/False by using: mysequence == 'Heads'. Once you have changed Heads/Tails to True/False, try figuring out a way to use the np.diff method followed by the np.sum method to count the number of changes.
SOURCE CODE IN PYTHON:
import numpy as np
#function to simulate a toss and return the value
def toss():
res=np.random.randint(2)
if res==0:
return 'Heads'
else:
return 'Tails'
#to store the result of tosses
tosses=[]
#simulating 100 tosses and storing True for Heads and False for tails
for i in range(100):
tosses.append(toss()=='Heads')
#np.diff() returns a list of boolean values that indicate
#if there were different values in adjacent cells
#np.sum() returns the number of True values, hence returning
#number of changes
changes=np.sum(np.diff(tosses))
#output
print('No. of changes:', changes)

OUTPUT:

Python: Write a bit of code that will simulate 100 random, independent coin flips. Write code...