mutate: add 10% change of adding and 10% of removing

This commit is contained in:
Starbeamrainbowlabs 2022-02-02 01:53:27 +00:00
parent ec58e75d83
commit 277b500f21
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -8,9 +8,15 @@ function random_item(arr) {
} }
export default function(word) { export default function(word) {
console.log(`***** MUTATE *****`)
const chars = word.toLowerCase().split(""); const chars = word.toLowerCase().split("");
const targetpos = Math.floor(Math.random() * word.length); const targetpos = Math.floor(Math.random() * word.length);
let mode = "replace";
if(Math.random() < 0.1) mode = "add";
if(Math.random() > 0.9) mode = "remove";
switch(mode) {
case "replace":
const targetchar = chars[targetpos]; const targetchar = chars[targetpos];
console.log("TARGET", targetchar, "POS", targetpos); console.log("TARGET", targetchar, "POS", targetpos);
let pool = consonants.concat(vowels); let pool = consonants.concat(vowels);
@ -29,5 +35,15 @@ export default function(word) {
let newchar = random_item(pool); let newchar = random_item(pool);
chars.splice(targetpos, 1, newchar); chars.splice(targetpos, 1, newchar);
break;
case "add":
chars.splice(targetpos, 0, random_item(consonants.concat(vowels)));
break;
case "remove":
chars.splice(targetpos, 1);
break;
}
return chars.join(""); return chars.join("");
} }