SEXP randExprGrowRecursive(struct RandExprGrowContext * TreeParams, int currentDepth) { if ((unif_rand() <= TreeParams->probSubtree)&&(currentDepth < TreeParams->maxDepth)) { const int funIdx= randIndex(TreeParams->nFunctions); const int arity= TreeParams->arities[funIdx]; SEXP expr; PROTECT(expr= R_NilValue); for ( int i=0; i < arity; i++ ) { SEXP newParameter; PROTECT(newParameter = randExprGrowRecursive(TreeParams, currentDepth+1)); PROTECT(expr= LCONS(newParameter, expr)); } PROTECT(expr= LCONS(install(TreeParams->functions[funIdx]), expr)); UNPROTECT(2 + 2*arity); return expr; } else if (unif_rand() <= TreeParams->constProb){ //create constant unif_rand(); // TODO constant factory return randomNumber(TreeParams->constScaling); } else { const int varIdx= randIndex(TreeParams->nVariables); //create variable SEXP expr2; PROTECT(expr2= install(TreeParams->variables[varIdx])); UNPROTECT(1); return expr2; } }
/* Perform a Monte Carlo sweep */ static TaskSignal monteCarloTaskTick(void *state) { assert(state != NULL); MonteCarloState *mcs = (MonteCarloState*) state; MonteCarloConfig *mcc = &mcs->conf; assert(mcc->delta > 0); for (int i = 0; i < world.numParticles; i++) { Particle *p = &world.particles[randIndex(world.numParticles)]; Vec3 oldPos = p->pos; p->pos.x += mcc->delta * (rand01() - 1/2.0); p->pos.y += mcc->delta * (rand01() - 1/2.0); if (!world.twoDimensional) p->pos.z += mcc->delta * (rand01() - 1/2.0); reboxParticle(p); if (collides(p)) { /* Back to old position! */ p->pos = oldPos; reboxParticle(p); } else { mcs->accepted++; } } mcs->attempted += world.numParticles; return TASK_OK; }
string ltest::goodbye(const string name) { string goodbyes[] = {"Goodbye", "Hej då", "Sayonara", "¡Adiós", "Adieu", "Ciao", "Tchüss", "Au revoir", "Namaste"}; return goodbyes[randIndex(9)] + (name==""?"":", ") + name + "!"; }