Skip to content
Related Articles

Related Articles

Introduction to Theory of Evolution in Python
  • Last Updated : 21 Apr, 2020

The process of inheritance involves the transmission of characteristics from one generation to another in the form of genes. These genes actually have DNA which is the coded information about the characteristics. Due to inheritance, evolution takes place from one species to another. So we can conclude that evolution is a random process as it does not happen at one go but involves millions of years to happen. For example, it took millions of years to evolve from amoeba to Homosapien(human beings).

The randomness attached to this process of evolution can be simulated. We will simulate this model in a very different way. Actually we can simulate and see, how evolution happened. In fact, there is a model of the ‘Genetic algorithm‘ in which it learns how the amoeba has evolved to Homosapien’s. So to understand the concept, here we will not use this algorithm because it will become much complicated to understand for beginners.

So to understand the concept of evolution, we will take a very large binary number containing only zeroes then we will convert all the zeroes to ones randomly using the random library. You will be shocked to get the result that after some time all the zeroes will be converted to ones.
The time taken to convert every zero to ones will be based on the

  • length of the string
  • The probability with which we convert zeroes to ones

Below is the implementation. Let’s suppose the process of evolution takes 500 times.

filter_none

edit
close

play_arrow

link
brightness_4
code

import random
  
  
def evolve(x):
      
    # index of the bit in x(list) 
    # in which change will happen
    i = random.randint(0, len(x)-1)
      
    # p is one then only change 
    # will happen
    p = random.randint(1, 5)
    if(p == 1):
          
        if(x[i] == 0):
            x[i] = 1
              
        else:
            x[i] = 1
              
# Driver code
x =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  
# the process of evolution
# will take place 500 times
for j in range(0, 500):
    evolve(x)
      
print(x)

chevron_right


Output:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :