Changeset 896 for cpp/frams/genetics/f9


Ignore:
Timestamp:
11/30/19 01:30:22 (4 years ago)
Author:
Maciej Komosinski
Message:

Replaced #defined macros for popular random-related operations with functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f9/f9_oper.cpp

    r779 r896  
    55#include "f9_oper.h"
    66#include "f9_conv.h"
    7 #include <common/nonstd.h> //randomN, rnd01
     7#include <common/nonstd.h> //rndUint, rnd01
    88
    99
     
    5555        for (int i = 0; i < len; i++)
    5656        {
    57                 if (rnd01 < mut_prob) //normalize prob with the length of the genotype
     57                if (rndDouble(1) < mut_prob) //normalize prob with the length of the genotype
    5858                {
    5959                        char oldgene = gene[i];
    60                         gene[i] = turtle_commands_f9[randomN(symbols)];
     60                        gene[i] = turtle_commands_f9[rndUint(symbols)];
    6161                        if (gene[i] != oldgene) changes++;
    6262                }
    6363        }
    6464
    65         if (rnd01 < mut_prob) //add or delete a random char
     65        if (rndDouble(1) < mut_prob) //add or delete a random char
    6666        {
    6767                SString newgeno(gene);
    68                 if (randomN(2) == 0) //add
     68                if (rndUint(2) == 0) //add
    6969                {
    7070                        int symbols = strlen(turtle_commands_f9);
    71                         int p = randomN(len + 1);  //random location
     71                        int p = rndUint(len + 1);  //random location
    7272                        //printf("before add: %s\n",(const char*)newgeno);
    73                         newgeno = newgeno.substr(0, p) + SString(turtle_commands_f9 + randomN(symbols), 1) + newgeno.substr(p);
     73                        newgeno = newgeno.substr(0, p) + SString(turtle_commands_f9 + rndUint(symbols), 1) + newgeno.substr(p);
    7474                        //printf("after add: %s\n",(const char*)newgeno);
    7575                        changes++;
     
    7777                else if (len > 1) //delete
    7878                {
    79                         int p = randomN(len);  //random location
     79                        int p = rndUint(len);  //random location
    8080                        //printf("before delete: %s\n",(const char*)newgeno);
    8181                        newgeno = newgeno.substr(0, p) + newgeno.substr(p + 1);
     
    9595{
    9696        int len1 = strlen(g1), len2 = strlen(g2);
    97         int p1 = randomN(len1);  //random cut point for first genotype
    98         int p2 = randomN(len2);  //random cut point for second genotype
     97        int p1 = rndUint(len1);  //random cut point for first genotype
     98        int p2 = rndUint(len2);  //random cut point for second genotype
    9999        char *child1 = (char*)malloc(p1 + len2 - p2 + 1);
    100100        char *child2 = (char*)malloc(p2 + len1 - p1 + 1);
Note: See TracChangeset for help on using the changeset viewer.