How to replace random parts of string with random keys from array?

Pavel Vanchugov

The code below is replacing random characters from the string, I am trying to make it replace parts of string from an array of words.

genetic.mutate = function(entity) {
    function replaceAt(str, index, character) {
        return str.substr(0, index) + character + str.substr(index+character.length);
    }

    // chromosomal drift
    var i = Math.floor(Math.random()*entity.length) 
    console.log(replaceAt(entity, i, String.fromCharCode(entity.charCodeAt(i) + (Math.floor(Math.random()*2) ? 1 : -1))));  
    return replaceAt(entity, i, String.fromCharCode(entity.charCodeAt(i) + (Math.floor(Math.random()*2) ? 1 : -1)));
};

The entity is a string of random characters with a length of "solution" text field value. Mutate function is using "charCode" + math random to find characters closer to a solution, and later in the fitness function, it gives fitness points to the algorithm, if it close to a solution. How to change mutate function, so it would try random keys from a set of the array, that contain all the words from the solution?

Here is the demo

https://codepen.io/anon/pen/MvzZPj?editors=1000

Any help would be appreciated!

Nina Scholz

You could split the string for an array and update at a special index and join the array for a new string.

function replace(string) {
    var array = string.split(''),
        i = Math.floor(Math.random() * array.length);

    array[i] = String.fromCharCode(array[i].charCodeAt(0) + 2 * Math.floor(Math.random() * 2) - 1);
    return array.join('');
}

console.log(replace('123456abcdef'));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to convert array keys into random string in php laravel

分類Dev

How to find the random value for a particular string in an array

分類Dev

How to extract a substring from a random string?

分類Dev

How to use <random> to replace rand()?

分類Dev

Random color from array in Sass

分類Dev

Set random backgroundColor from array

分類Dev

Random Hex colour from an array

分類Dev

Selecting a random variable from a array

分類Dev

Replace string keys with array elements

分類Dev

How to get the keys as string array from map?

分類Dev

How to choose a random Value from Assoc array then display the key and value

分類Dev

Shuffle an Array of String using random() [Swift, Linux]

分類Dev

pick a random item from a javascript array

分類Dev

Pick random element from an array and remove it

分類Dev

Javascript Next、Prev、Random from a array

分類Dev

PHP Random generator from array issues

分類Dev

How would I install a random program from a random website?

分類Dev

How to put random string along with loading effect

分類Dev

How to generate a random string in a profile alias?

分類Dev

How to limit digits quantity on a random String?

分類Dev

how to get random element of array which is not null?

分類Dev

Replace a certain group in a pyspark dataframe column with a random item from a list

分類Dev

How to select a continuous sample at random from a list?

分類Dev

how to get random entry from db?

分類Dev

How to exclude a number from Random number of series?

分類Dev

how to get random row values from a table and storing it in an array to be called individually

分類Dev

how would you make it so that it grabs a random number and uses it to grab objects from an array

分類Dev

Create a Random String

分類Dev

Picking a random phrase in an array

Related 関連記事

  1. 1

    How to convert array keys into random string in php laravel

  2. 2

    How to find the random value for a particular string in an array

  3. 3

    How to extract a substring from a random string?

  4. 4

    How to use <random> to replace rand()?

  5. 5

    Random color from array in Sass

  6. 6

    Set random backgroundColor from array

  7. 7

    Random Hex colour from an array

  8. 8

    Selecting a random variable from a array

  9. 9

    Replace string keys with array elements

  10. 10

    How to get the keys as string array from map?

  11. 11

    How to choose a random Value from Assoc array then display the key and value

  12. 12

    Shuffle an Array of String using random() [Swift, Linux]

  13. 13

    pick a random item from a javascript array

  14. 14

    Pick random element from an array and remove it

  15. 15

    Javascript Next、Prev、Random from a array

  16. 16

    PHP Random generator from array issues

  17. 17

    How would I install a random program from a random website?

  18. 18

    How to put random string along with loading effect

  19. 19

    How to generate a random string in a profile alias?

  20. 20

    How to limit digits quantity on a random String?

  21. 21

    how to get random element of array which is not null?

  22. 22

    Replace a certain group in a pyspark dataframe column with a random item from a list

  23. 23

    How to select a continuous sample at random from a list?

  24. 24

    how to get random entry from db?

  25. 25

    How to exclude a number from Random number of series?

  26. 26

    how to get random row values from a table and storing it in an array to be called individually

  27. 27

    how would you make it so that it grabs a random number and uses it to grab objects from an array

  28. 28

    Create a Random String

  29. 29

    Picking a random phrase in an array

ホットタグ

アーカイブ