text: Q:
  
  Simple Mutation with a Probability
  
  As per the section of code below, I am trying to implement an automatic and random mutation process.
  data = [0,1,0,0,0,0,0,1,0,0,1,1,0,0,1]
  
  data[random.randint(0,len(data)-1)]=random.randrange(0,1)
  
  print(data)
  
  The code is an adaptation of some other posts I have found, although it is randomly mutating a value every time with either a 0 or 1. I require this to occur with only a certain probability (such as a 0.05 chance of mutation) rather than always being guaranteed.
  Additionally, often a 0 is being replaced with a 0 and therefore there is no change to the output, so I would like to limit it in a way that a 0 will only mutate to a 1 and a 1 mutates to a 0.
  I would really appreciate the assistance in resolving these two issues.
  
  A:
  
  Resume
  
  mutate any value with a choosen probability
  randomly choose the position
  when the position is choosen, switch between 0 and 1
  
  def mutate(data, proba=0.05):
      if random.random() < proba:
          data[random.randrange(len(data))] ^= 1    
  
  if __name__ == '__main__':
      data = [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1]
      for i in range(10):
          mutate(data)
          print(data)
  
  A:
  
  You can do as following:
  For each element in data, mutate it (1 - val) only if a random value generated by random() function is less than the defined mutation probability.
  For example:
  import random
  
  mutation_prob = 0.05
  
  data = [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1]
  
  mutated_data = [1 - x if random.random() < mutation_prob else x for x in data]
  
  If the mutation should be decided regarding the data as a whole, you can do:
  mutation_prob = 0.05
  
  data = [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1]
  
  do_mutation = random.random() < mutation_prob
  mutated_data = [1 - x if do_mutation else x for x in data]
  
  