Created
August 21, 2017 22:16
-
-
Save NicolleLouis/82835d740c01c3de2dd6c4069398f31c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def mutateWord(word): | |
index_modification = int(random.random() * len(word)) | |
if (index_modification == 0): | |
word = chr(97 + int(26 * random.random())) + word[1:] | |
else: | |
word = word[:index_modification] + chr(97 + int(26 * random.random())) + word[index_modification+1:] | |
return word | |
def mutatePopulation(population, chance_of_mutation): | |
for i in range(len(population)): | |
if random.random() * 100 < chance_of_mutation: | |
population[i] = mutateWord(population[i]) | |
return population |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment