Changeset 896


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

Location:
cpp
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd_math.cpp

    r867 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
    55#include "nonstd_math.h"
    66
    7 RandomGenerator& rndGetInstance()
     7RandomGenerator &rndGetInstance()
    88{
    99        static RandomGenerator rnd(0);
  • cpp/common/nonstd_math.h

    r835 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2121//random number generator:
    2222#include "random.h"
    23 RandomGenerator& rndGetInstance();
     23RandomGenerator &rndGetInstance();
    2424
    25 #define rnd01 (rndGetInstance().getDouble())
    26 #define rnd0N(limit) (rndGetInstance().getDouble()*(limit))
    27 #define randomN(limit) ((unsigned int)(rndGetInstance().getDouble()*(limit)))
    28 #define setRandomSeed(seed) rndGetInstance().setSeed(seed)
    29 #define setRandomRandomSeed() rndGetInstance().randomize()
    30 
     25inline double rndDouble(double limit_exclusive) { return rndGetInstance().getDouble() * limit_exclusive; }
     26inline unsigned int rndUint(unsigned int limit_exclusive) { return (unsigned int)(rndGetInstance().getDouble() * limit_exclusive); } //returns random from 0..limit_exclusive-1
     27inline void rndSetSeed(unsigned int seed) { rndGetInstance().setSeed(seed); }
     28inline unsigned int rndRandomizeSeed() { return rndGetInstance().randomize(); }
    3129
    3230//floating point specific numbers
     
    6664void fpExceptDisable();
    6765
     66// std::lerp can be used since c++20 (and has some guaranteed properties probably better than this basic formula) but apparently it is not a template
     67template <typename Value, typename Linear> Value universal_lerp(Value a,Value b,Linear t) {return a*(1-t)+b*t;}
     68
    6869#endif
    6970
  • cpp/common/random.h

    r886 r896  
    22// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
     4#ifndef _COMMON_RANDOM_H_
     5#define _COMMON_RANDOM_H_
    46
    57#ifdef _MSC_VER
     
    140142        }
    141143};
     144#endif
  • cpp/frams/_demos/genomanipulation.cpp

    r742 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    4747        printf("      Current value of '%s' (%s) is '%s'\n", pi.id(i), pi.name(i), pi.get(i).c_str());
    4848        char t[100];
    49         sprintf(t, "%g", minprop + (rnd01)*(maxprop - minprop));
     49        sprintf(t, "%g", minprop + rndDouble(maxprop - minprop));
    5050        printf("      Setting new value... [ using ParamInterface::set() ]\n");
    5151        pi.setFromString(i, t);
  • cpp/frams/canvas/nn_simple_layout.cpp

    r789 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2626        for (i = 0; i < N; i++)
    2727        {
    28                 nn->SetXYWH(i, (int)rnd0N(300), (int)rnd0N(300), 50, 50);
     28                nn->SetXYWH(i, rndUint(300), rndUint(300), 50, 50);
    2929        }
    3030}
  • cpp/frams/genetics/f4/f4_general.cpp

    r853 r896  
    12101210        int n = count();
    12111211        // pick a random node, between 0 and n-1
    1212         return ordNode(randomN(n));
     1212        return ordNode(rndUint(n));
    12131213}
    12141214
     
    13551355{
    13561356        int i, j, res, t;
    1357         char tc1, tc2, tc3; // tc3 is only to ensure that in the end  of neuron parameter definition
     1357        char tc1, tc2, tc3; // tc3 is only to ensure that neuron parameter definition is completed
    13581358        int relfrom;
    13591359        double w;
  • cpp/frams/genetics/f4/f4_oper.cpp

    r779 r896  
    158158                        // "X>" or "N>"
    159159                        f4_node *n5 = NULL;
    160                         double pr = rnd01;
     160                        double pr = rndDouble(1);
    161161                        pr -= 0.5;
    162162                        if (pr < 0) n5 = new f4_node('X', n2, n2->pos);
     
    204204                        n1->parent = n2;
    205205                        // now with 50% chance swap children
    206                         if (randomN(2) == 0)
     206                        if (rndUint(2) == 0)
    207207                        {
    208208                                n3 = n2->child;
     
    275275                        // choose a simple node from ADD_SIMPLE_CODES
    276276                        n1->parent->removeChild(n1);
    277                         //f4_node *n2 = new f4_node(ADD_SIMPLE_CODES[randomN(strlen(ADD_SIMPLE_CODES))], n1->parent, n1->parent->pos);
     277                        //f4_node *n2 = new f4_node(ADD_SIMPLE_CODES[rndUint(strlen(ADD_SIMPLE_CODES))], n1->parent, n1->parent->pos);
    278278                        int modifierid = GenoOperators::getRandomChar(all_modifiers, excluded_modifiers.c_str());
    279279                        f4_node *n2 = new f4_node(all_modifiers[modifierid], n1->parent, n1->parent->pos);
     
    333333                                n2->removeChild(n1);
    334334                                // n1 has two children. pick one randomly 50-50, destroy other
    335                                 if (randomN(2) == 0)
     335                                if (rndUint(2) == 0)
    336336                                {
    337337                                        n1->child->parent = n2;
     
    429429
    430430        // 35% chance one of *GTS
    431         prob1 = rnd01;
     431        prob1 = rndDouble(1);
    432432        prob1 -= 0.35f;
    433433        if (prob1 < 0)
     
    468468        case 0: // change type
    469469                // 80% for link, 20% for random sensor
    470                 if (rnd01 < 0.2f)
     470                if (rndDouble(1) < 0.2f)
    471471                {
    472472                        cl = GenoOperators::getRandomNeuroClassWithOutputAndNoInputs();
     
    497497void Geno_f4::nparNodeMakeRandom(f4_node *nn) const
    498498{
    499         int sign = (int)(2.0f * rnd01);
    500         int param = (int)(3.0f * rnd01);
     499        int sign = (int)rndDouble(2);
     500        int param = (int)rndDouble(3);
    501501        if (param > 2) param = 2;
    502502        nn->l1 = sign;
     
    512512        // change count
    513513        count = nn->i1;
    514         prob1 = rnd01;
     514        prob1 = rndDouble(1);
    515515        if (prob1 < 0.5f) count++;
    516516        else count--;
     
    699699        // decide amounts of crossover, 0.25-0.75
    700700        // adam: seems 0.1-0.9 -- MacKo
    701         chg1 = 0.1f + 0.8f*rnd01;
    702         chg2 = 0.1f + 0.8f*rnd01;
     701        chg1 = 0.1 + rndDouble(0.8);
     702        chg2 = 0.1 + rndDouble(0.8);
    703703
    704704        copy1 = root1.duplicate();
  • 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);
  • cpp/frams/genetics/fB/fB_oper.cpp

    r853 r896  
    246246        {
    247247                std::list<SString> tokenized = tokenizeSequence(line);
    248                 int rndid = randomN(tokenized.size()); // select random letter from genotype
     248                int rndid = rndUint(tokenized.size()); // select random letter from genotype
    249249                // increment/decrement character - when overflow happens, this method
    250250                // uses reflect method
     
    254254                if ((*it).len() == 1)
    255255                {
    256                         if (randomN(2) == 0)
     256                        if (rndUint(2) == 0)
    257257                        {
    258258                                if ((*it)[0] == 'a') (*it).directWrite()[0] = 'b';
     
    285285                std::list<SString> tokenized = tokenizeSequence(line);
    286286                std::list<SString>::iterator it = tokenized.begin();
    287                 int rndid = randomN(tokenized.size()); // select random insertion point
     287                int rndid = rndUint(tokenized.size()); // select random insertion point
    288288                std::advance(it, rndid);
    289289                NeuroClass *cls = getRandomNeuroClass();
     
    306306                chg = 1.0 / line.len();
    307307                std::list<SString> tokenized = tokenizeSequence(line);
    308                 int rndid = randomN(tokenized.size()); // select random insertion point
     308                int rndid = rndUint(tokenized.size()); // select random insertion point
    309309                std::list<SString>::iterator it = tokenized.begin();
    310310                std::advance(it, rndid);
    311311                SString letter = "a";
    312                 letter.directWrite()[0] = 'a' + randomN(26);
     312                letter.directWrite()[0] = 'a' + rndUint(26);
    313313                tokenized.insert(it, letter);
    314314                line = detokenizeSequence(&tokenized);
     
    320320                std::list<SString> tokenized = tokenizeSequence(line);
    321321                std::list<SString>::iterator it = tokenized.begin();
    322                 int rndid = randomN(tokenized.size()); // select random deletion point
     322                int rndid = rndUint(tokenized.size()); // select random deletion point
    323323                std::advance(it, rndid);
    324324                tokenized.erase(it);
     
    328328        case FB_DUPLICATION:
    329329        {
    330                 int rndgene = randomN(fB_GenoHelpers::geneCount(line));
     330                int rndgene = rndUint(fB_GenoHelpers::geneCount(line));
    331331                int start, end;
    332332                SString gene = fB_GenoHelpers::getGene(rndgene, line, start, end);
     
    342342                for (int i = 0; i < 4; i++)
    343343                {
    344                         cuts[i] = randomN(tokenized.size());
     344                        cuts[i] = rndUint(tokenized.size());
    345345                }
    346346                std::sort(cuts.begin(), cuts.end());
     
    407407        {
    408408                // get random gene from first parent
    409                 int choice = randomN(fB_GenoHelpers::geneCount(parent1));
     409                int choice = rndUint(fB_GenoHelpers::geneCount(parent1));
    410410                int start, end;
    411411                SString gene = fB_GenoHelpers::getGene(choice, parent1, start, end);
     
    414414                chg2 = (float)parent2.len() / (float)child2.len();
    415415                // do the same for second parent
    416                 choice = randomN(fB_GenoHelpers::geneCount(parent2));
     416                choice = rndUint(fB_GenoHelpers::geneCount(parent2));
    417417                gene = fB_GenoHelpers::getGene(choice, parent2, start, end);
    418418                child1 = gene + parent1;
     
    428428        //                      int start, end;
    429429        //                      SString gene = fB_GenoHelpers::getGene(i, parent1, start, end);
    430         //                      if (randomN(2) == 0)
     430        //                      if (rndUint(2) == 0)
    431431        //                      {
    432432        //                              child1 += gene;
     
    445445        //                      int start, end;
    446446        //                      SString gene = fB_GenoHelpers::getGene(i, parent2, start, end);
    447         //                      if (randomN(2) == 0)
     447        //                      if (rndUint(2) == 0)
    448448        //                      {
    449449        //                              child1 += gene;
     
    474474                                i < fB_GenoHelpers::geneCountNoNested(parent2))
    475475                        {
    476                                 if (randomN(2) == 0)
     476                                if (rndUint(2) == 0)
    477477                                {
    478478                                        to1 = fB_GenoHelpers::getNonNestedGene(i, parent1, start, end);
     
    489489                        else if (i < fB_GenoHelpers::geneCountNoNested(parent1))
    490490                        {
    491                                 if (randomN(2) == 0)
     491                                if (rndUint(2) == 0)
    492492                                {
    493493                                        to1 = fB_GenoHelpers::getNonNestedGene(i, parent1, start, end);
     
    501501                        else // if (i < fB_GenoHelpers::geneCountNoNested(parent2))
    502502                        {
    503                                 if (randomN(2) == 0)
     503                                if (rndUint(2) == 0)
    504504                                {
    505505                                        to1 = fB_GenoHelpers::getNonNestedGene(i, parent2, start, end);
  • cpp/frams/genetics/fF/fF_oper.cpp

    r779 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
    55#include "fF_oper.h"
    66#include "fF_genotype.h"
    7 #include <common/nonstd.h> //randomN, rnd01
     7#include <common/nonstd.h> //rndUint, rnd01
    88
    99
     
    4949        par.load(gene);
    5050        static const int propsToMutate[] = fF_PROPS_TO_MUTATE;
    51         int which = randomN(ARRAY_LENGTH(propsToMutate));
     51        int which = rndUint(ARRAY_LENGTH(propsToMutate));
    5252        bool mutated_ok = GenoOperators::mutatePropertyNaive(par.param, propsToMutate[which]);
    5353        if (mutated_ok)
  • cpp/frams/genetics/fH/fH_oper.cpp

    r803 r896  
    107107        for (unsigned int i = 0; i < parent1->sticks.size(); i++)
    108108        {
    109                 if (randomN(2) == 0)
     109                if (rndUint(2) == 0)
    110110                {
    111111                        child1->sticks.push_back(parent1->sticks[i]);
     
    120120        for (unsigned int i = 0; i < parent2->sticks.size(); i++)
    121121        {
    122                 if (randomN(2) == 0)
     122                if (rndUint(2) == 0)
    123123                {
    124124                        child1->sticks.push_back(parent2->sticks[i]);
     
    140140        for (unsigned int i = 0; i < parent1->neurons.size(); i++)
    141141        {
    142                 if ((randomN(2) == 0 || skip2) && !skip1)
     142                if ((rndUint(2) == 0 || skip2) && !skip1)
    143143                {
    144144                        child1->neurons.push_back(parent1->neurons[i]);
     
    153153        for (unsigned int i = 0; i < parent2->neurons.size(); i++)
    154154        {
    155                 if ((randomN(2) == 0 || skip2) && !skip1)
     155                if ((rndUint(2) == 0 || skip2) && !skip1)
    156156                {
    157157                        child1->neurons.push_back(parent2->neurons[i]);
     
    166166        for (unsigned int i = 0; i < parent1->connections.size(); i++)
    167167        {
    168                 if ((randomN(2) == 0 || skip2) && !skip1)
     168                if ((rndUint(2) == 0 || skip2) && !skip1)
    169169                {
    170170                        child1->connections.push_back(parent1->connections[i]);
     
    179179        for (unsigned int i = 0; i < parent2->connections.size(); i++)
    180180        {
    181                 if ((randomN(2) == 0 || skip2) && !skip1)
     181                if ((rndUint(2) == 0 || skip2) && !skip1)
    182182                {
    183183                        child1->connections.push_back(parent2->connections[i]);
     
    338338        if (changedimensions)
    339339        {
    340                 int i = randomN(2 * dimensions);
     340                int i = rndUint(2 * dimensions);
    341341                changeDoubleProperty(i, par, handle->type);
    342342        }
     
    344344        if (changeproperties)
    345345        {
    346                 int i = 2 * dimensions + randomN(par.getPropCount() - 2 * dimensions);
     346                int i = 2 * dimensions + rndUint(par.getPropCount() - 2 * dimensions);
    347347                changeDoubleProperty(i, par, handle->type);
    348348        }
     
    360360        for (int i = 0; i < dimensions; i++)
    361361        {
    362                 par.setDouble(i, min + rnd0N(max - min));
    363                 par.setDouble(i + dimensions, min + rnd0N(max - min));
     362                par.setDouble(i, min + rndDouble(max - min));
     363                par.setDouble(i + dimensions, min + rndDouble(max - min));
    364364        }
    365365        handle->loadProperties(par);
    366366        if (handle->type != fHBodyType::NEURON)
    367367        {
    368                 int i = 2 * dimensions + randomN(par.getPropCount() - 2 * dimensions);
     368                int i = 2 * dimensions + rndUint(par.getPropCount() - 2 * dimensions);
    369369                changeDoubleProperty(i, par, handle->type);
    370370        }
     
    406406                allhandlescount += creature->sticks.size();
    407407        }
    408         unsigned int toselect = randomN(allhandlescount);
     408        unsigned int toselect = rndUint(allhandlescount);
    409409        if (toselect < creature->connections.size())
    410410        {
     
    436436        if (par.getPropCount() > 0)
    437437        {
    438                 int i = randomN(par.getPropCount());
     438                int i = rndUint(par.getPropCount());
    439439                if (*par.type(i) == 'f')
    440440                {
  • cpp/frams/genetics/fL/fL_matheval.cpp

    r821 r896  
    613613void MathEvaluation::mutateValueOrVariable(MathEvaluation::Number *&currval, bool usetime)
    614614{
    615         if (randomN(2) == 0 && varcount > 0) // use variable
     615        if (rndUint(2) == 0 && varcount > 0) // use variable
    616616        {
    617617                if (currval && currval->type == TokenType::NUMBER)
     
    619619                        delete currval;
    620620                }
    621                 int var = randomN(varcount + (usetime ? 1 : 0));
     621                int var = rndUint(varcount + (usetime ? 1 : 0));
    622622                if (varcount == var) // time is used
    623623                {
     
    633633                if (!currval || currval->type == TokenType::VARIABLE)
    634634                {
    635                         currval = new Number(rnd01);
     635                        currval = new Number(rndDouble(1));
    636636                }
    637637                else
    638638                {
    639                         currval->value = rnd01;
     639                        currval->value = rndDouble(1);
    640640                }
    641641        }
     
    654654                count = operatorstrings.size() - arithmeticoperatorscount;
    655655        }
    656         randop += randomN(count);
     656        randop += rndUint(count);
    657657        return operators[operatorstrings[randop]];
    658658}
     
    663663        {
    664664                int currsize = postfixlist.size();
    665                 int varid = randomN(varcount);
     665                int varid = rndUint(varcount);
    666666                postfixlist.push_back(vars[varid]);
    667                 if (randomN(2) == 0 && varcount > 1)
    668                 {
    669                         int varid2 = randomN(varcount - 1);
     667                if (rndUint(2) == 0 && varcount > 1)
     668                {
     669                        int varid2 = rndUint(varcount - 1);
    670670                        if (varid2 >= varid) varid2++;
    671671                        postfixlist.push_back(vars[varid2]);
     
    673673                else
    674674                {
    675                         Number *num = new Number(rnd01);
     675                        Number *num = new Number(rndDouble(1));
    676676                        postfixlist.push_back(num);
    677677                }
    678                 int opid = arithmeticoperatorscount + randomN(comparisonoperatorscount);
     678                int opid = arithmeticoperatorscount + rndUint(comparisonoperatorscount);
    679679                postfixlist.push_back(operators[operatorstrings[opid]]);
    680680                if (currsize > 0)
     
    689689        if (postfixlist.size() == 0)
    690690        {
    691                 Number *val = new Number(rnd01);
     691                Number *val = new Number(rndDouble(1));
    692692                postfixlist.push_back(val);
    693693                return -1;
    694694        }
    695         int method = randomN(postfixlist.size() < MAX_MUT_FORMULA_SIZE ? MATH_MUT_COUNT : MATH_MUT_COUNT - 1);
     695        int method = rndUint(postfixlist.size() < MAX_MUT_FORMULA_SIZE ? MATH_MUT_COUNT : MATH_MUT_COUNT - 1);
    696696        switch (method)
    697697        {
     
    704704                std::list<Token *>::iterator it = postfixlist.begin();
    705705                // insertion can be applied from 1st occurrence
    706                 int insertlocation = 1 + randomN(postfixlist.size() - 1);
     706                int insertlocation = 1 + rndUint(postfixlist.size() - 1);
    707707                std::advance(it, insertlocation);
    708708                Operator *rndop;
     
    732732                        id++;
    733733                }
    734                 int randid = randomN(numbersineval.size());
     734                int randid = rndUint(numbersineval.size());
    735735                Number *numptr = (Number *)(*numbersineval[randid]);
    736736                mutateValueOrVariable(numptr, usetime);
     
    750750                if (ops.size() > 0)
    751751                {
    752                         int randid = randomN(ops.size());
     752                        int randid = rndUint(ops.size());
    753753                        Operator *rndop;
    754754                        if (randid == (int)ops.size() - 1)
     
    783783                if (firstofpairs.size() > 0)
    784784                {
    785                         int rndid = randomN(firstofpairs.size());
     785                        int rndid = rndUint(firstofpairs.size());
    786786                        if ((*firstofpairs[rndid])->type == TokenType::NUMBER)
    787787                        {
  • cpp/frams/genetics/fL/fL_oper.cpp

    r853 r896  
    133133                if (newword->name.startsWith("rot"))
    134134                {
    135                         double rot = 2 * rnd01;
     135                        double rot = rndDouble(2);
    136136                        MathEvaluation *eval = new MathEvaluation(0);
    137137                        eval->convertString(SString::valueOf(rot).c_str());
     
    149149                {
    150150                        MathEvaluation *eval = new MathEvaluation(0);
    151                         eval->convertString(SString::valueOf(2 * rnd01 - 1).c_str());
     151                        eval->convertString(SString::valueOf(rndDouble(2) - 1).c_str());
    152152                        newword->parevals[0] = eval;
    153153                }
     
    171171        else
    172172        {
    173                 int rid = randomN(creature->rules.size());
     173                int rid = rndUint(creature->rules.size());
    174174                list = &creature->rules[rid]->objsucc;
    175175                numparams = creature->rules[rid]->objpred->npar;
     
    183183        if (method == FL_ADD_OTHER && creature->builtincount < (int)creature->words.size())
    184184        {
    185                 return creature->words[creature->wordnames[creature->builtincount + randomN((int)creature->words.size() - creature->builtincount)]];
     185                return creature->words[creature->wordnames[creature->builtincount + rndUint((int)creature->words.size() - creature->builtincount)]];
    186186        }
    187187        else
     
    209209                case FL_ADD_ROT:
    210210                {
    211                         int rottype = randomN(3);
     211                        int rottype = rndUint(3);
    212212                        switch (rottype)
    213213                        {
     
    287287                case FL_CHG_ITER:
    288288                {
    289                         if (randomN(2) == 0)
     289                        if (rndUint(2) == 0)
    290290                        {
    291291                                creature->time = creature->time + iterchangestep <= ExtValue::getDouble(FL_MAXITER) ?
     
    303303                        if (creature->rules.size() > 0)
    304304                        {
    305                                 int ruleid = randomN(creature->rules.size());
     305                                int ruleid = rndUint(creature->rules.size());
    306306                                if (!creature->rules[ruleid]->condeval)
    307307                                {
     
    340340                        if (wordswithnorules.size() > 0)
    341341                        {
    342                                 int predid = randomN(wordswithnorules.size());
     342                                int predid = rndUint(wordswithnorules.size());
    343343                                fL_Rule *newrule = new fL_Rule(0,0);
    344344                                fL_Word *pred = new fL_Word();
     
    352352                        else if (creature->rules.size() > 0)
    353353                        {
    354                                 int ruleid = randomN(creature->rules.size());
     354                                int ruleid = rndUint(creature->rules.size());
    355355                                fL_Rule *newrule = new fL_Rule(0, 0);
    356356                                fL_Word *pred = new fL_Word();
     
    392392                        if (creature->countDefinedWords() <= maxdefinedwords)
    393393                        {
    394                                 int npar = randomN(ExtValue::getInt(FL_MAXPARAMS, false));
     394                                int npar = rndUint(ExtValue::getInt(FL_MAXPARAMS, false));
    395395                                for (int i = 0; i < maxdefinedwords; i++)
    396396                                {
     
    421421                                if (list->size() > 1)
    422422                                {
    423                                         int rndid = randomN(list->size() - 1);
     423                                        int rndid = rndUint(list->size() - 1);
    424424                                        int j = 0;
    425425                                        std::list<fL_Word *>::iterator it = list->begin();
     
    455455                        else
    456456                        {
    457                                 int rndid = randomN(list->size());
     457                                int rndid = rndUint(list->size());
    458458                                std::list<fL_Word *>::iterator it = list->begin();
    459459                                std::advance(it, rndid);
     
    482482                        int tmp = 0;
    483483                        std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp);
    484                         int rndid = randomN(list->size());
     484                        int rndid = rndUint(list->size());
    485485                        std::list<fL_Word *>::iterator it = list->begin();
    486486                        std::advance(it, rndid);
     
    512512                                fL_Branch *start = new fL_Branch(fL_Branch::BranchType::OPEN, 0, 0);
    513513                                list->insert(it, start);
    514                                 int rottype = randomN(2);
     514                                int rottype = rndUint(2);
    515515                                switch (rottype)
    516516                                {
     
    531531                        int tmp = 0;
    532532                        std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp);
    533                         int rndid = randomN(list->size());
     533                        int rndid = rndUint(list->size());
    534534                        std::list<fL_Word *>::iterator selectedword = list->begin();
    535535                        std::advance(selectedword, rndid);
     
    545545                                int numpars = 0;
    546546                                std::list<fL_Word *> *list = selectRandomSequence(creature, numpars, tmp);
    547                                 int rndid = randomN(list->size());
     547                                int rndid = rndUint(list->size());
    548548                                std::list<fL_Word *>::iterator it = list->begin();
    549549                                std::advance(it, rndid);
     
    577577                                        if (available.size() > 0)
    578578                                        {
    579                                                 int newnameid = randomN(available.size());
     579                                                int newnameid = rndUint(available.size());
    580580                                                (*selectedword)->name = available[newnameid]->name;
    581581                                        }
     
    593593                                if ((*selectedword)->npar > 0)
    594594                                {
    595                                         int randeval = randomN((*selectedword)->npar);
     595                                        int randeval = rndUint((*selectedword)->npar);
    596596                                        Param par((*selectedword)->tab, (*selectedword)->data);
    597597                                        if ((*selectedword)->builtin && (*selectedword)->name == "N"
     
    614614                                                        if (w->npar > 0)
    615615                                                        {
    616                                                                 int rndattr = randomN(w->npar);
     616                                                                int rndattr = rndUint(w->npar);
    617617                                                                if (!w->parevals[rndattr])
    618618                                                                {
     
    638638                                                                if (w->npar > 0)
    639639                                                                {
    640                                                                         int rndattr = randomN(w->npar);
     640                                                                        int rndattr = rndUint(w->npar);
    641641                                                                        for (int i = 0; i < w->npar; i++)
    642642                                                                        {
     
    752752                for (int i = 0; i < numselrules; i++)
    753753                {
    754                         int rulid = randomN(from->rules.size());
     754                        int rulid = rndUint(from->rules.size());
    755755                        fL_Rule *rul = from->rules[rulid];
    756756                        fL_Rule *newrule = new fL_Rule(0, 0);
     
    841841        creature2template->parseGenotype(g2);
    842842
    843         int numselrules = 1 + randomN(XOVER_MAX_MIGRATED_RULES);
     843        int numselrules = 1 + rndUint(XOVER_MAX_MIGRATED_RULES);
    844844        numselrules = numselrules < (int)creature1->rules.size() ? numselrules : (int)creature1->rules.size();
    845845
    846846        migrateRandomRules(creature1template, creature2, numselrules);
    847847
    848         numselrules = 1 + randomN(XOVER_MAX_MIGRATED_RULES);
     848        numselrules = 1 + rndUint(XOVER_MAX_MIGRATED_RULES);
    849849        numselrules = numselrules < (int)creature1->rules.size() ? numselrules : (int)creature1->rules.size();
    850850
  • cpp/frams/genetics/fT/fTest_oper.cpp

    r779 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    8888        int changes = 0, len = strlen(geno);
    8989        for (int i = 0; i < len; i++)
    90                 if (rnd01 < prob) //normalize prob with length of genotype
     90                if (rndDouble(1) < prob) //normalize prob with length of genotype
    9191                {
    92                         geno[i] = a[randomN(4)];
     92                        geno[i] = a[rndUint(4)];
    9393                        changes++;
    9494                }
     
    101101{
    102102        int len1 = strlen(g1), len2 = strlen(g2);
    103         int p1 = randomN(len1);  //random cut point for first genotype
    104         int p2 = randomN(len2);  //random cut point for second genotype
     103        int p1 = rndUint(len1);  //random cut point for first genotype
     104        int p2 = rndUint(len2);  //random cut point for second genotype
    105105        char *child1 = (char*)malloc(p1 + len2 - p2 + 1);
    106106        char *child2 = (char*)malloc(p2 + len1 - p1 + 1);
  • cpp/frams/genetics/fn/fn_oper.cpp

    r809 r896  
    55#include "fn_oper.h"
    66#include "fn_conv.h"
    7 #include <common/nonstd.h> //randomN, rnd01
     7#include <common/nonstd.h> //rndUint, rnd01
    88
    99
     
    8080        if (mut_single_var) //mutate only one, randomly selected variable
    8181        {
    82                 int which = randomN(values.size());
     82                int which = rndUint(values.size());
    8383                values[which] = GenoOperators::mutateCreep('f', values[which], bound_low[which], bound_high[which], stddev[which], false);
    8484                chg = 1.0f / values.size();
     
    103103        //xover_proportion = 0.1; //testing...
    104104
    105         double proportion = xover_proportion_random ? 0.5 + rnd0N(0.5) : xover_proportion;
     105        double proportion = xover_proportion_random ? 0.5 + rndDouble(0.5) : xover_proportion;
    106106
    107107        chg1 = proportion;
  • cpp/frams/genetics/genman.cpp

    r841 r896  
    362362                {
    363363                        char *gn;
    364                         if (g1n[0] && g2n[0]) if (randomN(2) == 0) g1n[0] = 0; else g2n[0] = 0; //both provided? we want only one
     364                        if (g1n[0] && g2n[0]) if (rndUint(2) == 0) g1n[0] = 0; else g2n[0] = 0; //both provided? we want only one
    365365                        if (g1n[0]) { gn = g1n; chg = chg1; }
    366366                        else { gn = g2n; chg = chg2; }
  • cpp/frams/genetics/genooperators.cpp

    r801 r896  
    3838        int i;
    3939        for (i = 0; i < count; i++) sum += probtab[i];
    40         double sel = rnd01*sum;
     40        double sel = rndDouble(sum);
    4141        for (sum = 0, i = 0; i < count; i++) { sum += probtab[i]; if (sel < sum) return i; }
    4242        return -1;
     
    8181                neucls = n->getClass() == NULL ? 0 : n->getClass()->getProperties().getPropCount();
    8282        if (neuext + neucls == 0) return -1; //no properties in this neuron
    83         int index = randomN(neuext + neucls);
     83        int index = rndUint(neuext + neucls);
    8484        if (index >= neuext) index = index - neuext + 100;
    8585        return index;
     
    142142        {
    143143                result = int(result + 0.5);
    144                 if (result == current) result += randomN(2) * 2 - 1; //force some change
     144                if (result == current) result += rndUint(2) * 2 - 1; //force some change
    145145        }
    146146        else
     
    238238                if (Neuro::getClass(i)->genactive)
    239239                        active.push_back(Neuro::getClass(i));
    240         if (active.size() == 0) return NULL; else return active[randomN(active.size())];
     240        if (active.size() == 0) return NULL; else return active[rndUint(active.size())];
    241241}
    242242
     
    247247                if (Neuro::getClass(i)->genactive && Neuro::getClass(i)->getPreferredOutput() != 0)
    248248                        active.push_back(Neuro::getClass(i));
    249         if (active.size() == 0) return NULL; else return active[randomN(active.size())];
     249        if (active.size() == 0) return NULL; else return active[rndUint(active.size())];
    250250}
    251251
     
    256256                if (Neuro::getClass(i)->genactive && Neuro::getClass(i)->getPreferredInputs() != 0)
    257257                        active.push_back(Neuro::getClass(i));
    258         if (active.size() == 0) return NULL; else return active[randomN(active.size())];
     258        if (active.size() == 0) return NULL; else return active[rndUint(active.size())];
    259259}
    260260
     
    265265                if (Neuro::getClass(i)->genactive && Neuro::getClass(i)->getPreferredOutput() != 0 && Neuro::getClass(i)->getPreferredInputs() == 0)
    266266                        active.push_back(Neuro::getClass(i));
    267         if (active.size() == 0) return NULL; else return active[randomN(active.size())];
     267        if (active.size() == 0) return NULL; else return active[rndUint(active.size())];
    268268}
    269269
     
    274274                if (NClist[i]->getPreferredOutput() != 0) //this NeuroClass provides output
    275275                        allowed.push_back(i);
    276         if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())];
     276        if (allowed.size() == 0) return -1; else return allowed[rndUint(allowed.size())];
    277277}
    278278
     
    283283                if (NClist[i]->getPreferredInputs() != 0) //this NeuroClass wants one input connection or more                 
    284284                        allowed.push_back(i);
    285         if (allowed.size() == 0) return -1; else return allowed[randomN(allowed.size())];
     285        if (allowed.size() == 0) return -1; else return allowed[rndUint(allowed.size())];
    286286}
    287287
     
    291291        for (size_t i = 0; i < strlen(choices); i++) if (!strchrn0(excluded, choices[i])) allowed_count++;
    292292        if (allowed_count == 0) return -1; //no char is allowed
    293         int rnd_index = randomN(allowed_count) + 1;
     293        int rnd_index = rndUint(allowed_count) + 1;
    294294        allowed_count = 0;
    295295        for (size_t i = 0; i < strlen(choices); i++)
  • cpp/frams/model/model.cpp

    r815 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    8383        updateRefno();
    8484        if (using_checkpoints)
    85                 for (vector<Model*>::const_iterator it = mod.checkpoints.begin(); it != mod.checkpoints.end(); it++)
    86                 {
    87                 Model *m = *it;
    88                 Model *n = new Model(*m, m->autobuildmaps, false, true);
    89                 checkpoints.push_back(n);
     85                for (vector<Model *>::const_iterator it = mod.checkpoints.begin(); it != mod.checkpoints.end(); it++)
     86                {
     87                        Model *m = *it;
     88                        Model *n = new Model(*m, m->autobuildmaps, false, true);
     89                        checkpoints.push_back(n);
    9090                }
    9191}
     
    152152void Model::clear()
    153153{
    154         FOREACH(Part*, p, parts)
     154        FOREACH(Part *, p, parts)
    155155                delete p;
    156         FOREACH(Joint*, j, joints)
     156        FOREACH(Joint *, j, joints)
    157157                delete j;
    158         FOREACH(Neuro*, n, neurons)
     158        FOREACH(Neuro *, n, neurons)
    159159                delete n;
    160160        parts.clear(); joints.clear(); neurons.clear();
     
    164164        geno = Geno();
    165165        f0geno = Geno();
    166         for (vector<Model*>::iterator it = checkpoints.begin(); it != checkpoints.end(); it++)
     166        for (vector<Model *>::iterator it = checkpoints.begin(); it != checkpoints.end(); it++)
    167167                delete *it;
    168168        checkpoints.clear();
     
    195195void Model::removeNeuros(SList &nlist)
    196196{
    197         FOREACH(Neuro*, nu, nlist)
     197        FOREACH(Neuro *, nu, nlist)
    198198        {
    199199                int i = findNeuro(nu);
     
    209209                SList jlist;
    210210                findJoints(jlist, p);
    211                 FOREACH(Joint*, j, jlist)
     211                FOREACH(Joint *, j, jlist)
    212212                {
    213213                        int i = findJoint(j);
     
    240240void Model::removeNeuro(int neuroindex, bool removereferences)
    241241{
    242         Neuro* thisN = getNeuro(neuroindex);
     242        Neuro *thisN = getNeuro(neuroindex);
    243243
    244244        if (removereferences)
    245245        {
    246                 Neuro* n;
     246                Neuro *n;
    247247                // remove all references to thisN
    248                 for (int i = 0; n = (Neuro*)neurons(i); i++)
     248                for (int i = 0; n = (Neuro *)neurons(i); i++)
    249249                {
    250250                        Neuro *inp;
     
    252252                                if (inp == thisN)
    253253                                {
    254                                 n->removeInput(j);
    255                                 j--;
     254                                        n->removeInput(j);
     255                                        j--;
    256256                                }
    257257                }
     
    262262}
    263263
    264 MultiMap& Model::getMap()
     264MultiMap &Model::getMap()
    265265{
    266266        if (!map) map = new MultiMap();
     
    277277}
    278278
    279 void Model::makeGenToGenMap(MultiMap& result, const MultiMap& gen1tomodel, const MultiMap& gen2tomodel)
     279void Model::makeGenToGenMap(MultiMap &result, const MultiMap &gen1tomodel, const MultiMap &gen2tomodel)
    280280{
    281281        result.clear();
     
    285285}
    286286
    287 void Model::getCurrentToF0Map(MultiMap& result)
     287void Model::getCurrentToF0Map(MultiMap &result)
    288288{
    289289        result.clear();
    290290        if (!map) return;
    291         const MultiMap& f0m = getF0Map();
     291        const MultiMap &f0m = getF0Map();
    292292        makeGenToGenMap(result, *map, f0m);
    293293}
     
    312312}
    313313
    314 Model::ItemType Model::itemTypeFromLinePrefix(const char* line)
    315 {
    316         struct PrefixAndItem { const char* prefix; ItemType type; };
     314Model::ItemType Model::itemTypeFromLinePrefix(const char *line)
     315{
     316        struct PrefixAndItem { const char *prefix; ItemType type; };
    317317        static const PrefixAndItem types[] = { { "m:", ModelType }, { "p:", PartType }, { "j:", JointType }, { "n:", NeuronType }, { "c:", NeuronConnectionType }, { F0_CHECKPOINT_LINE, CheckpointType }, { NULL } };
    318318        for (const PrefixAndItem *t = types; t->prefix != NULL; t++)
    319319        {
    320                 const char* in = line;
    321                 const char* pattern = t->prefix;
     320                const char *in = line;
     321                const char *pattern = t->prefix;
    322322                for (; *in == *pattern; in++, pattern++)
    323323                        if (*pattern == ':')
     
    355355        for (; f0txt.getNextToken(pos, line, '\n'); lnum++)
    356356        {
    357                 const char* line_ptr = line.c_str();
     357                const char *line_ptr = line.c_str();
    358358                for (; *line_ptr; line_ptr++)
    359359                        if (!strchr(" \r\t", *line_ptr)) break;
     
    361361                if (!*line_ptr) continue;
    362362
    363                 const char* colon = strchr(line_ptr, ':');
     363                const char *colon = strchr(line_ptr, ':');
    364364                ItemType type = UnknownType;
    365365                SString excluding_prefix;
     
    414414}
    415415
    416 const MultiMap& Model::getF0Map()
     416const MultiMap &Model::getF0Map()
    417417{
    418418        if (!f0map)
     
    472472        }
    473473
    474         for (i = 0; p = (Part*)parts(i); i++)
     474        for (i = 0; p = (Part *)parts(i); i++)
    475475        {
    476476                partparam.select(p);
     
    481481                        map->add(len, gen.len() - 1, partToMap(i));
    482482        }
    483         for (i = 0; j = (Joint*)joints(i); i++)
     483        for (i = 0; j = (Joint *)joints(i); i++)
    484484        {
    485485                jointparam.select(j);
     
    491491                        map->add(len, gen.len() - 1, jointToMap(i));
    492492        }
    493         for (i = 0; n = (Neuro*)neurons(i); i++)
     493        for (i = 0; n = (Neuro *)neurons(i); i++)
    494494        {
    495495                neuroparam.select(n);
     
    502502        for (a = 0; a < neurons.size(); a++)
    503503        { // inputs
    504                 n = (Neuro*)neurons(a);
     504                n = (Neuro *)neurons(a);
    505505                //      if ((n->getInputCount()==1)&&(n->getInput(0).refno <= n->refno))
    506506                //              continue; // already done with Neuro::conn_refno
     
    510510                        double w;
    511511                        NeuroConn nc;
    512                         Neuro* n2 = n->getInput(b, w);
     512                        Neuro *n2 = n->getInput(b, w);
    513513                        //              if (((n2.parentcount==1)&&(n2.parent)&&(n2.parent->refno < n2.refno)) ^
    514514                        //                  (n2.neuro_refno>=0))
     
    533533        }
    534534
    535         for (vector<Model*>::const_iterator it = checkpoints.begin(); it != checkpoints.end(); it++)
     535        for (vector<Model *>::const_iterator it = checkpoints.begin(); it != checkpoints.end(); it++)
    536536        {
    537537                Geno g = (*it)->getF0Geno();
     
    562562}
    563563
    564 Model* Model::getCheckpoint(int i)
     564Model *Model::getCheckpoint(int i)
    565565{
    566566        return checkpoints[i];
     
    575575}
    576576
    577 void Model::setGeno(const Geno& newgeno)
     577void Model::setGeno(const Geno &newgeno)
    578578{
    579579        geno = newgeno;
     
    586586        delMap();
    587587        delF0Map();
    588         for (i = 0; p = (Part*)parts(i); i++)
     588        for (i = 0; p = (Part *)parts(i); i++)
    589589                p->clearMapping();
    590         for (i = 0; j = (Joint*)joints(i); i++)
     590        for (i = 0; j = (Joint *)joints(i); i++)
    591591                j->clearMapping();
    592         for (i = 0; n = (Neuro*)neurons(i); i++)
     592        for (i = 0; n = (Neuro *)neurons(i); i++)
    593593                n->clearMapping();
    594594}
     
    607607                        Part *p; Joint *j; Neuro *n;
    608608                        int i;
    609                         for (i = 0; p = (Part*)parts(i); i++)
     609                        for (i = 0; p = (Part *)parts(i); i++)
    610610                                if (p->getMapping())
    611611                                        map->add(*p->getMapping(), partToMap(i));
    612                         for (i = 0; j = (Joint*)joints(i); i++)
     612                        for (i = 0; j = (Joint *)joints(i); i++)
    613613                                if (j->getMapping())
    614614                                        map->add(*j->getMapping(), jointToMap(i));
    615                         for (i = 0; n = (Neuro*)neurons(i); i++)
     615                        for (i = 0; n = (Neuro *)neurons(i); i++)
    616616                                if (n->getMapping())
    617617                                        map->add(*n->getMapping(), neuroToMap(i));
     
    629629}
    630630
    631 Pt3D Model::whereDelta(const Part& start, const Pt3D& rot, const Pt3D& delta)
     631Pt3D Model::whereDelta(const Part &start, const Pt3D &rot, const Pt3D &delta)
    632632{
    633633        Orient roto;
     
    641641}
    642642
    643 int Model::addFromString(ItemType item_type, const SString &singleline, const MultiRange* srcrange)
     643int Model::addFromString(ItemType item_type, const SString &singleline, const MultiRange *srcrange)
    644644{
    645645        return addFromString(item_type, singleline, 0, srcrange);
    646646}
    647647
    648 int Model::addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange* srcrange)
     648int Model::addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange *srcrange)
    649649{
    650650        SString error_message;
     
    654654                if (error_message.len() == 0) // generic error when no detailed message is available
    655655                        error_message = "Invalid f0 code";
    656                 if (line_num>0)
     656                if (line_num > 0)
    657657                        error_message += SString::sprintf(", line #%d", line_num);
    658658                error_message += nameForErrors();
     
    662662}
    663663
    664 int Model::addFromStringNoLog(ItemType item_type, const SString &line, SString& error_message, const MultiRange* srcrange)
     664int Model::addFromStringNoLog(ItemType item_type, const SString &line, SString &error_message, const MultiRange *srcrange)
    665665{
    666666        error_message = SString::empty();
     
    831831SString Model::nameForErrors() const
    832832{
    833         if (geno.getName().len()>0)
     833        if (geno.getName().len() > 0)
    834834                return SString::sprintf(" in '%s'", geno.getName().c_str());
    835835        return SString::empty();
     
    850850        else
    851851        {
    852                 Pt3D bbmin = ((Part*)parts(0))->p, bbmax = bbmin;
     852                Pt3D bbmin = ((Part *)parts(0))->p, bbmax = bbmin;
    853853                for (i = 0; i < parts.size(); i++)
    854854                {
    855                         p = (Part*)parts(i);
     855                        p = (Part *)parts(i);
    856856                        p->owner = this;
    857857                        if (checklevel > 0)
     
    894894                for (i = 0; i < joints.size(); i++)
    895895                {
    896                         j = (Joint*)joints(i);
     896                        j = (Joint *)joints(i);
    897897                        // VALIDMINMAX are managed manually when adding joint properties in f0-def!
    898898                        // (could be made dynamic but not really worth the effort)
     
    979979        for (i = 0; i < neurons.size(); i++)
    980980        {
    981                 n = (Neuro*)neurons(i);
     981                n = (Neuro *)neurons(i);
    982982                n->part_refno = (n->part) ? n->part->refno : -1;
    983983                n->joint_refno = (n->joint) ? n->joint->refno : -1;
     
    991991                        for (i = 0; i < parts.size(); i++)
    992992                        {
    993                                 p = (Part*)parts(i);
     993                                p = (Part *)parts(i);
    994994                                if (p->mass <= 0.001)
    995995                                        p->mass = 1.0;
     
    10031003                                for (i = 0; i < joints.size(); i++)
    10041004                                {
    1005                                         j = (Joint*)joints(i);
    1006                                         if (j->part1->flags&LINKFLAG)
     1005                                        j = (Joint *)joints(i);
     1006                                        if (j->part1->flags & LINKFLAG)
    10071007                                        {
    1008                                                 if (!(j->part2->flags&LINKFLAG))
     1008                                                if (!(j->part2->flags & LINKFLAG))
    10091009                                                {
    10101010                                                        change = 1;
     
    10131013                                        }
    10141014                                        else
    1015                                                 if (j->part2->flags&LINKFLAG)
     1015                                                if (j->part2->flags & LINKFLAG)
    10161016                                                {
    1017                                                 if (!(j->part1->flags&LINKFLAG))
    1018                                                 {
    1019                                                         change = 1;
    1020                                                         j->part1->flags |= LINKFLAG;
    1021                                                 }
     1017                                                        if (!(j->part1->flags & LINKFLAG))
     1018                                                        {
     1019                                                                change = 1;
     1020                                                                j->part1->flags |= LINKFLAG;
     1021                                                        }
    10221022                                                }
    10231023                                }
     
    10251025                        for (i = 0; i < parts.size(); i++)
    10261026                        {
    1027                                 p = (Part*)parts(i);
    1028                                 if (!(p->flags&LINKFLAG))
     1027                                p = (Part *)parts(i);
     1028                                if (!(p->flags & LINKFLAG))
    10291029                                {
    10301030                                        logPrintf("Model", "internalCheck", LOG_ERROR, "Not all parts connected (eg. Part #0 and Part #%d)%s", i, nameForErrors().c_str());
     
    10371037                for (i = 0; i < joints.size(); i++)
    10381038                {
    1039                         j = (Joint*)joints(i);
     1039                        j = (Joint *)joints(i);
    10401040                        if (j->p1_refno == j->p2_refno)
    10411041                        {
     
    10461046                        for (k = i + 1; k < joints.size(); k++)
    10471047                        {
    1048                                 Joint* j2 = (Joint*)joints(k);
     1048                                Joint *j2 = (Joint *)joints(k);
    10491049                                if (((j->p1_refno == j2->p1_refno) && (j->p2_refno == j2->p2_refno))
    10501050                                        || ((j->p1_refno == j2->p2_refno) && (j->p2_refno == j2->p1_refno)))
     
    10731073}
    10741074
    1075 const Geno& Model::getGeno() const
     1075const Geno &Model::getGeno() const
    10761076{
    10771077        return geno;
     
    11061106}
    11071107
    1108 Part* Model::getPart(int i) const
    1109 {
    1110         return ((Part*)parts(i));
     1108Part *Model::getPart(int i) const
     1109{
     1110        return ((Part *)parts(i));
    11111111}
    11121112
     
    11161116}
    11171117
    1118 Joint* Model::getJoint(int i) const
    1119 {
    1120         return ((Joint*)joints(i));
    1121 }
    1122 
    1123 int Model::findJoints(SList& result, const Part* part)
     1118Joint *Model::getJoint(int i) const
     1119{
     1120        return ((Joint *)joints(i));
     1121}
     1122
     1123int Model::findJoints(SList &result, const Part *part)
    11241124{
    11251125        Joint *j;
    11261126        int n0 = result.size();
    11271127        if (part)
    1128                 for (int i = 0; j = (Joint*)joints(i); i++)
    1129                         if ((j->part1 == part) || (j->part2 == part)) result += (void*)j;
     1128                for (int i = 0; j = (Joint *)joints(i); i++)
     1129                        if ((j->part1 == part) || (j->part2 == part)) result += (void *)j;
    11301130        return result.size() - n0;
    11311131}
    11321132
    1133 int Model::findNeuro(Neuro* n)
     1133int Model::findNeuro(Neuro *n)
    11341134{
    11351135        return neurons.find(n);
    11361136}
    11371137
    1138 int Model::findPart(Part* p)
     1138int Model::findPart(Part *p)
    11391139{
    11401140        return parts.find(p);
    11411141}
    11421142
    1143 int Model::findJoint(Joint* j)
     1143int Model::findJoint(Joint *j)
    11441144{
    11451145        return joints.find(j);
     
    11481148int Model::findJoint(Part *p1, Part *p2)
    11491149{
    1150         Joint* j;
     1150        Joint *j;
    11511151        for (int i = 0; j = getJoint(i); i++)
    11521152                if ((j->part1 == p1) && (j->part2 == p2)) return i;
     
    11611161}
    11621162
    1163 Neuro* Model::getNeuro(int i) const
    1164 {
    1165         return (Neuro*)neurons(i);
     1163Neuro *Model::getNeuro(int i) const
     1164{
     1165        return (Neuro *)neurons(i);
    11661166}
    11671167
     
    11741174}
    11751175
    1176 int Model::findNeuros(SList& result,
    1177         const char* classname, const Part* part, const Joint* joint)
     1176int Model::findNeuros(SList &result,
     1177        const char *classname, const Part *part, const Joint *joint)
    11781178{
    11791179        Neuro *nu;
    11801180        SString cn(classname);
    11811181        int n0 = result.size();
    1182         for (int i = 0; nu = (Neuro*)neurons(i); i++)
     1182        for (int i = 0; nu = (Neuro *)neurons(i); i++)
    11831183        {
    11841184                if (part)
     
    11881188                if (classname)
    11891189                        if (nu->getClassName() != cn) continue;
    1190                 result += (void*)nu;
     1190                result += (void *)nu;
    11911191        }
    11921192        return result.size() - n0;
     
    12021202        {
    12031203                Part *p = getPart(i);
    1204                 p->p.x += (rnd01 - 0.5)*amount;
    1205                 p->p.y += (rnd01 - 0.5)*amount;
    1206                 p->p.z += (rnd01 - 0.5)*amount;
     1204                p->p.x += (rndDouble(1) - 0.5) * amount;
     1205                p->p.y += (rndDouble(1) - 0.5) * amount;
     1206                p->p.z += (rndDouble(1) - 0.5) * amount;
    12071207        }
    12081208        for (i = 0; i < joints.size(); i++)
     
    12151215}
    12161216
    1217 void Model::move(const Pt3D& shift)
    1218 {
    1219         FOREACH(Part*, p, parts)
     1217void Model::move(const Pt3D &shift)
     1218{
     1219        FOREACH(Part *, p, parts)
    12201220                p->p += shift;
    12211221}
    12221222
    1223 void Model::rotate(const Orient& rotation)
    1224 {
    1225         FOREACH(Part*, p, parts)
     1223void Model::rotate(const Orient &rotation)
     1224{
     1225        FOREACH(Part *, p, parts)
    12261226        {
    12271227                p->p = rotation.transform(p->p);
     
    12301230}
    12311231
    1232 void Model::buildUsingSolidShapeTypes(const Model& src_ballandstick_shapes, Part::Shape use_shape, double thickness)
     1232void Model::buildUsingSolidShapeTypes(const Model &src_ballandstick_shapes, Part::Shape use_shape, double thickness)
    12331233{
    12341234        for (int i = 0; i < src_ballandstick_shapes.getJointCount(); i++)
     
    12471247                for (int i = 0; i < src_ballandstick_shapes.getPartCount(); i++)
    12481248                {
    1249                 Part *op = src_ballandstick_shapes.getPart(i);
    1250                 Part *p = addNewPart(Part::SHAPE_ELLIPSOID); //always using spherical shape regardless of the 'use_shape' parameter - 'use shape' is meant for sticks!
    1251                 p->p = op->p;
    1252                 p->rot = op->rot;
    1253                 p->scale.x = p->scale.y = p->scale.z = thickness;
     1249                        Part *op = src_ballandstick_shapes.getPart(i);
     1250                        Part *p = addNewPart(Part::SHAPE_ELLIPSOID); //always using spherical shape regardless of the 'use_shape' parameter - 'use shape' is meant for sticks!
     1251                        p->p = op->p;
     1252                        p->rot = op->rot;
     1253                        p->scale.x = p->scale.y = p->scale.z = thickness;
    12541254                }
    12551255        for (int i = 0; i < src_ballandstick_shapes.getPartCount(); i++)
     
    12751275}
    12761276
    1277 SolidsShapeTypeModel::SolidsShapeTypeModel(Model& m, Part::Shape use_shape, double thickness)
     1277SolidsShapeTypeModel::SolidsShapeTypeModel(Model &m, Part::Shape use_shape, double thickness)
    12781278{
    12791279        using_model = converted_model = NULL;
     
    13341334class MaxNeuro : public Neuro { public: MaxNeuro() { Param par(f0_neuro_paramtab, this); par.setMax(); } };
    13351335
    1336 Part& Model::getMinPart() { static MinPart part; return part; }
    1337 Part& Model::getMaxPart() { static MaxPart part; return part; }
    1338 Part& Model::getDefPart() { static Part part; return part; }
    1339 Joint& Model::getMinJoint() { static MinJoint joint; return joint; }
    1340 Joint& Model::getMaxJoint() { static MaxJoint joint; return joint; }
    1341 Joint& Model::getDefJoint() { static Joint joint; return joint; }
    1342 Neuro& Model::getMinNeuro() { static MinNeuro neuro; return neuro; }
    1343 Neuro& Model::getMaxNeuro() { static MaxNeuro neuro; return neuro; }
    1344 Neuro& Model::getDefNeuro() { static Neuro neuro; return neuro; }
     1336Part &Model::getMinPart() { static MinPart part; return part; }
     1337Part &Model::getMaxPart() { static MaxPart part; return part; }
     1338Part &Model::getDefPart() { static Part part; return part; }
     1339Joint &Model::getMinJoint() { static MinJoint joint; return joint; }
     1340Joint &Model::getMaxJoint() { static MaxJoint joint; return joint; }
     1341Joint &Model::getDefJoint() { static Joint joint; return joint; }
     1342Neuro &Model::getMinNeuro() { static MinNeuro neuro; return neuro; }
     1343Neuro &Model::getMaxNeuro() { static MaxNeuro neuro; return neuro; }
     1344Neuro &Model::getDefNeuro() { static Neuro neuro; return neuro; }
  • cpp/frams/model/model.h

    r815 r896  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    7777        SList parts, joints, neurons;
    7878        char partmappingchanged;
    79         vector<Model*> checkpoints;
     79        vector<Model *> checkpoints;
    8080
    8181        void internalCopy(const Model &mod);
     
    141141
    142142        int getCheckpointCount();
    143         Model* getCheckpoint(int i);
     143        Model *getCheckpoint(int i);
    144144
    145145        /// The bounding box size. Valid if the model is valid. Read only.
     
    201201
    202202        /// change source genotype
    203         void setGeno(const Geno& newgeno);
     203        void setGeno(const Geno &newgeno);
    204204
    205205        /** @return f0 genotype - generated from current model state
     
    241241                @see convmap
    242242                */
    243         void getCurrentToF0Map(MultiMap& m);
     243        void getCurrentToF0Map(MultiMap &m);
    244244
    245245        void setValidationLevel(int level)
     
    250250        /// calculate location of the new part connected to the existing one
    251251        /// using delta option
    252         Pt3D whereDelta(const Part& start, const Pt3D& rot, const Pt3D& delta);
     252        Pt3D whereDelta(const Part &start, const Pt3D &rot, const Pt3D &delta);
    253253
    254254        /// create the whole model from scratch, using current genotype
     
    256256
    257257        /// setGeno(newgeno); rebuild();
    258         void rebuild(const Geno& newgeno, bool buildmaps) { setGeno(newgeno); rebuild(buildmaps); }
     258        void rebuild(const Geno &newgeno, bool buildmaps) { setGeno(newgeno); rebuild(buildmaps); }
    259259
    260260        /// reuse current model object but discard all model data
     
    262262
    263263        enum ItemType { UnknownType, ModelType, PartType, JointType, NeuronType, NeuronConnectionType, CheckpointType };
    264         static ItemType itemTypeFromLinePrefix(const char* line);
     264        static ItemType itemTypeFromLinePrefix(const char *line);
    265265        /** Execute single line of <B>f0</B> genotype.
    266266                Return value is non-negative reference number of the created item,
     
    270270                @param srcrange source genotype range which will be mapped to this element
    271271                */
    272         int addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange* srcrange = NULL);
     272        int addFromString(ItemType item_type, const SString &singleline, int line_num, const MultiRange *srcrange = NULL);
    273273        /** Execute single line of <B>f0</B> genotype - compatiblity variant */
    274         int addFromString(ItemType item_type, const SString &singleline, const MultiRange* srcrange = NULL);
     274        int addFromString(ItemType item_type, const SString &singleline, const MultiRange *srcrange = NULL);
    275275        /** Execute single line of <B>f0</B> genotype - low level variant, used by Model::build(), error messages returned as string instead of calling logger */
    276         int addFromStringNoLog(ItemType item_type, const SString &singleline, SString& error_message, const MultiRange* srcrange = 0);
     276        int addFromStringNoLog(ItemType item_type, const SString &singleline, SString &error_message, const MultiRange *srcrange = 0);
    277277
    278278        /// separate build stages (for future use)
     
    357357
    358358        /// @return part index or -1 if not found in the model
    359         int findPart(Part* p);
     359        int findPart(Part *p);
    360360        /// @return joint index or -1 if not found in the model
    361         int findJoint(Joint* j);
     361        int findJoint(Joint *j);
    362362        /// @return neuro index or -1 if not found in the model
    363         int findNeuro(Neuro* nu);
     363        int findNeuro(Neuro *nu);
    364364        /// @return joint index or -1 if not found in the model
    365365        int findJoint(Part *p1, Part *p2);
     
    368368                @param result objects will be appended here
    369369                @return number of objects found  */
    370         int findNeuros(SList& result, const char* classname = 0, const Part* part = 0, const Joint* joint = 0);
     370        int findNeuros(SList &result, const char *classname = 0, const Part *part = 0, const Joint *joint = 0);
    371371
    372372        /** search for joints connected to the part
    373373                @param result objects will be appended here
    374374                @return number of objects found  */
    375         int findJoints(SList& result, const Part* part = 0);
     375        int findJoints(SList &result, const Part *part = 0);
    376376
    377377        void disturb(double amount);
    378         void move(const Pt3D& shift);
     378        void move(const Pt3D &shift);
    379379        /// rotate around the origin (move-rotate-move to rotate around arbitrary point)
    380         void rotate(const Orient& rotation);
     380        void rotate(const Orient &rotation);
    381381        /// rotate around the origin (move-rotate-move to rotate around arbitrary point)
    382         void rotate(const Pt3D& angles) { Orient o = Orient_1; o.rotate(angles); rotate(o); }
     382        void rotate(const Pt3D &angles) { Orient o = Orient_1; o.rotate(angles); rotate(o); }
    383383
    384384        /// build this model using solid shape types, based on the provided ball-and-stick model. See also shapeconvert.cpp.
    385         void buildUsingSolidShapeTypes(const Model& src_ballandstick_shapes, Part::Shape use_shape = Part::SHAPE_CYLINDER, double thickness = 0.2);
     385        void buildUsingSolidShapeTypes(const Model &src_ballandstick_shapes, Part::Shape use_shape = Part::SHAPE_CYLINDER, double thickness = 0.2);
    386386
    387387protected:
     
    403403        static int mapToNeuro(int i);
    404404
    405         static void makeGenToGenMap(MultiMap& result, const MultiMap& gen1tomodel, const MultiMap& gen2tomodel);
     405        static void makeGenToGenMap(MultiMap &result, const MultiMap &gen1tomodel, const MultiMap &gen2tomodel);
    406406
    407407        ///////////////////////////
    408408
    409         static Part& getMinPart();
    410         static Part& getMaxPart();
    411         static Part& getDefPart();
    412         static Joint& getMinJoint();
    413         static Joint& getMaxJoint();
    414         static Joint& getDefJoint();
    415         static Neuro& getMinNeuro();
    416         static Neuro& getMaxNeuro();
    417         static Neuro& getDefNeuro();
     409        static Part &getMinPart();
     410        static Part &getMaxPart();
     411        static Part &getDefPart();
     412        static Joint &getMinJoint();
     413        static Joint &getMaxJoint();
     414        static Joint &getDefJoint();
     415        static Neuro &getMinNeuro();
     416        static Neuro &getMaxNeuro();
     417        static Neuro &getDefNeuro();
    418418};
    419419
     
    436436        Model *converted_model;
    437437        Model *using_model;
    438         SolidsShapeTypeModel(Model& m, Part::Shape use_shape = Part::SHAPE_CYLINDER, double thickness = 0.2);
    439         operator Model&() { return *using_model; }
    440         Model& getModel() { return *using_model; }
     438        SolidsShapeTypeModel(Model &m, Part::Shape use_shape = Part::SHAPE_CYLINDER, double thickness = 0.2);
     439        operator Model &() { return *using_model; }
     440        Model &getModel() { return *using_model; }
    441441        ~SolidsShapeTypeModel() { if (converted_model) delete converted_model; }
    442442};
  • cpp/frams/neuro/impl/neuroimpl-simple.h

    r791 r896  
    7777public:
    7878        NeuroImpl* makeNew() { return new NI_Random(); };
    79         void go() { setState(rnd01*2.0 - 1.0); }
     79        void go() { setState(rndDouble(2) - 1.0); }
    8080};
    8181
  • cpp/frams/neuro/neuroimpl.cpp

    r732 r896  
    8585        for (i = 0; n = mod.getNeuro(i); i++)
    8686        {
    87                 n->state += (rnd01 - 0.5)*config.randominit;
     87                n->state += (rndDouble(1) - 0.5)*config.randominit;
    8888                ni = (NeuroImpl*)n->userdata[mytags_id];
    8989                if (!ni) continue;
  • cpp/frams/util/rndutil.cpp

    r424 r896  
    2121double CustomRnd(double *tab)
    2222{
    23         double *range = tab + 1 + 2 * randomN((int)(0.5 + tab[0]));
    24         return range[0] + rnd0N(range[1] - range[0]);
     23        double *range = tab + 1 + 2 * rndUint((int)(0.5 + tab[0]));
     24        return range[0] + rndDouble(range[1] - range[0]);
    2525}
    2626
    2727double RandomGener::Uni(double begin, double end)
    2828{
    29         return begin + rnd01*(end - begin);
     29        return begin + rndDouble(end - begin);
    3030}
    3131
     
    3535        double v1, v2, s;
    3636        do {
    37                 v1 = 2 * rnd01 - 1; //-1..1
    38                 v2 = 2 * rnd01 - 1; //-1..1
     37                v1 = rndDouble(2) - 1; //-1..1
     38                v2 = rndDouble(2) - 1; //-1..1
    3939                s = v1*v1 + v2*v2;
    4040        } while (s >= 1);
Note: See TracChangeset for help on using the changeset viewer.