void mutateconnections(Genome* g) { for (int i = 0; i < g->ngenes; i++) { if (randfloat() < PerturbChance) g->genes[i].weight += (2*randfloat()-1) * StepSize; else g->genes[i].weight = 4*randfloat() - 2; } }
static float2 cmj(int s, int m, int n, int p) { int sx = permute(s % m, m, p * 0xa511e9b3); int sy = permute(s / m, n, p * 0x63d83595); float jx = randfloat(s, p * 0xa399d265); float jy = randfloat(s, p * 0x711ad6a5); return float2((s % m + (sy + jx) / n) / m, (s / m + (sx + jy) / m) / n); }
float randgaus(const float mean, const float sigma) { double x, y, r2; do { x = -1 + 2 * randfloat(); y = -1 + 2 * randfloat(); r2 = x * x + y * y; } while (r2 > 1.0 || r2 == 0); return (float) (sigma * y * sqrt(-2.0 * log(r2) / r2)) + mean; }
//generates a psuedo-random float between min and max float randfloat(float min, float max) { if (min>max) { return randfloat()*(min-max)+max; } else { return randfloat()*(max-min)+min; } }
void SaxHackSynthesizer::SynthesizeSample(std::vector<double> *instantParams) { /* these should be defined. eBreathPressure, eBlowPosition, eReedStiffness, eReedAperature, eVibratoFrequency, eVibratoGain, eNoiseGain, eMaxBlowLength, eMaxRestLength, control numbers: input.controlChange(11,64); //blow position input.controlChange(2,64); //reed stiffness input.controlChange(26,64); //reed ap input.controlChange(128,128); //breath pressure */ //sax.controlChange(128,(*instantParams)[eLoudness]); sax.setFrequency((*instantParams)[eCenterFrequency]); sax.controlChange(128,(*instantParams)[eBreathPressure]); sax.controlChange(11,(*instantParams)[eBlowPosition]); sax.controlChange(2,(*instantParams)[eReedStiffness]); sax.controlChange(26,(*instantParams)[eReedAperature]); sax.controlChange(29,(*instantParams)[eVibratoFrequency]); sax.controlChange(1,(*instantParams)[eVibratoGain]); sax.controlChange(4,(*instantParams)[eNoiseGain]); //should we stop blowing? //eMaxBlowLength etc in seconds if(noteOrRestLen++ > noteOrRestDur) { blowing=!blowing; if(blowing) sax.startBlowing(1.0,1.0/(randfloat(44100.0*(*instantParams)[eMaxBlowLength])+0.01)); //stop blowing at a rate proportional to the rest length else sax.stopBlowing(1.0/(44100.0*randfloat((*instantParams)[eMaxRestLength])+0.01)); //new duration? noteOrRestLen = 0; noteOrRestDur = randint((*instantParams)[blowing?eMaxBlowLength:eMaxRestLength] * 44100); } sax.controlChange(26,(*instantParams)[eReedAperature]); buffer->SetFrame(currentSample++,sax.tick() *SHORTLIMITF * (*instantParams)[eAmplitude]); }
//---------------------------------Mutate-------------------------------- // // mutates a chromosome by perturbing its weights by an amount not // greater than CParams::dMaxPerturbation //----------------------------------------------------------------------- void GenAlg::Mutate(vector<float> &chromo) { //traverse the chromosome and mutate each weight dependent //on the mutation rate for (int i=0; i<chromo.size(); ++i) { //do we perturb this weight? if (randfloat(0.0,1.0) < m_dMutationRate) { //add or subtract a small value to the weight chromo[i] += (randfloat(-1.0,1.0) * .3); } } }
void emit_frame(emit_desc* frame) { for (int i = 0; i < frame->n; ++i) { particle* p = NULL; float r = randfloat(frame->r - frame->ur, frame->r + frame->ur); float g = randfloat(frame->g - frame->ug, frame->g + frame->ug); float b = randfloat(frame->b - frame->ub, frame->b + frame->ub); pextra* pe = new_pextra(r, g, b, frame->blender); p = particle_new_full(frame->x, frame->y, frame->ux, frame->uy, frame->rad, frame->urad, frame->ds, frame->uds, frame->theta, frame->utheta, frame->life, frame->ulife, frame->force, frame->limit, pe); plist_add(emitter.particles, p); } }
void Interpolation::Truncate(double startTime, double duration, std::vector<TreeNode*> *activeNodes, std::vector<ParameterList*> *parameters, int layerNum, int topLayer, int numControlledLayers) { mStartTime = startTime; mDuration = duration; //make sure we fit within all parameter boundries for all associated paramter lists. double startValue = (*parameters)[layerNum]->GetValue(mParam); //- we don't know where it will be at the end of the leaf. //now check to see where the parameter will be at upon the end of the leaf. //we do this by asking each of the active nodes what their value will be at the end of THIS leaf //and multiply them against the start value double topValue = startValue; double bottomValue = startValue; for(int i =0;i<numControlledLayers;i++) { topValue = mymax(topValue, (*parameters)[topLayer+i]->GetValue(mParam)); bottomValue = mymin(bottomValue, (*parameters)[topLayer+i]->GetValue(mParam)); } for(int i=0;i<activeNodes->size();i++) { for(int j=0;j<((*activeNodes)[i])->GetNumInterpolations();j++) { if(((*activeNodes)[i])->GetInterpolation(j)->GetParameter() == mParam) { //we use this node's start time, and the parent node's end time (since we can't push them to overstep their boundries.) topValue = mymax(topValue,topValue*((*activeNodes)[i])->GetInterpolation(j)->MaxValueInTimeRange(mStartTime,((*activeNodes)[i])->GetInterpolation(j)->GetStartTime()+((*activeNodes)[i])->GetInterpolation(j)->GetDuration())); bottomValue = mymin(bottomValue,bottomValue*((*activeNodes)[i])->GetInterpolation(j)->MinValueInTimeRange(mStartTime,((*activeNodes)[i])->GetInterpolation(j)->GetStartTime()+((*activeNodes)[i])->GetInterpolation(j)->GetDuration())); } } } assert(topValue >= bottomValue); while(topValue*mEndCoef> (*parameters)[layerNum]->GetMaxValue(mParam)) mEndCoef= randfloat(mEndCoef-1.0)+1.0; while(bottomValue*mEndCoef<(*parameters)[layerNum]->GetMinValue(mParam)) mEndCoef = randfloat(1.0-mEndCoef)+mEndCoef; if((topValue*mEndCoef>(*parameters)[layerNum]->GetMaxValue(mParam)) || (bottomValue*mEndCoef<(*parameters)[layerNum]->GetMinValue(mParam)) ) mEndCoef=1.0; }
void HaarFtr::generate(FtrParams *op) { HaarFtrParams *p = (HaarFtrParams*)op; _width = p->_width; _height = p->_height; int numrects = randint(p->_minNumRect,p->_maxNumRect); _rects.resize(numrects); _weights.resize(numrects); _rsums.resize(numrects); _maxSum = 0.0f; for( int k=0; k<numrects; k++ ) { _weights[k] = randfloat()*2-1; _rects[k].x = randint(0,(uint)(p->_width-3)); _rects[k].y = randint(0,(uint)(p->_height-3)); _rects[k].width = randint(1,(p->_width-_rects[k].x-2)); _rects[k].height = randint(1 ,(p->_height-_rects[k].y-2)); _rsums[k] = abs(_weights[k]*(_rects[k].width+1)*(_rects[k].height+1)*255); //_rects[k].width = randint(1,3); //_rects[k].height = randint(1,3); } if( p->_numCh < 0 ){ p->_numCh=0; for( int k=0; k<1024; k++ ) p->_numCh += p->_useChannels[k]>=0; } _channel = p->_useChannels[randint(0,p->_numCh-1)]; }
bool HarvestingEffect::idle(const Uint64 usec) { if (particles.size() == 0) return false; effect_center.x = pos->x; effect_center.z = pos->z; switch (type) { case BEES: { const Uint64 age = get_time() - born; const float age_f = (float)(age)/1000000; if (age_f > 1.0f) direction.y = randfloat(1.25); effect_center = *pos + direction * (-1.0f + age_f); gravity_center = *pos + direction * (-1.0f + age_f); break; } default: { effect_center.y += usec / 3000000.0; gravity_center.y += usec / 10000000.0; break; } } return true; }
bool MissileParticle::idle(const Uint64 delta_t) { if (effect->recall) return false; if (alpha < 0.03) return false; //const alpha_t scalar = math_cache.powf_05_close((float)delta_t / 20000); alpha *= math_cache.powf_0_1_rough_close(randfloat(), delta_t / 2000000.0); // increase this number to make particles live longer velocity *= 1 / (1 + delta_t / 500000.0); // slow down particles velocity.y -= ((delta_t / 250000.0) * (delta_t / 250000.0)); // let particles drop /* * disabled by Roja's request :-( switch (type) { case MissileEffect::MAGIC: // make magic particles glitter color[0] = randcolor(1.0); color[1] = randcolor(1.0); color[2] = randcolor(1.0); break; default: break; } */ return true; }
/**************************************************************** SelectSamplesUniformlyFromLargerSet Select Exceptions: None ****************************************************************/ void SampleSet::SelectSamplesUniformlyFromLargerSet( int maximumNumberOfSamples ) { try { uint numberOfSamples = Size(); //random sample according to uniform distribution float probability = ( static_cast<float>(maximumNumberOfSamples+1) ) / numberOfSamples; int i = 0; if ( probability < 1 ) { // random pick maximumNumberOfSamples from all available samples while( i < m_sampleList.size() ) { if ( randfloat() > probability ) { //remove this sample m_sampleList.erase( m_sampleList.begin() + i ); } else { //keep this sample and move on the next one i++; } } m_sampleList.resize( min( i, maximumNumberOfSamples ) ); } } EXCEPTION_CATCH_AND_ABORT( "Failed to select samples uniformly from larger set." ); }
bool CandleEffect::idle(const Uint64 usec) { if ((recall) && (particles.size() == 0)) return false; if (recall) return true; while (((int)particles.size() < LOD * 20) && ((math_cache.powf_0_1_rough_close(randfloat(), (LOD * 20 - particles.size()) * (interval_t)usec / 80 / square(LOD)) < 0.5) || ((int)particles.size() < LOD * 10))) { Vec3 coords = spawner->get_new_coords(); coords.y += 0.1 * sqrt_scale; coords += *pos; Vec3 velocity; velocity.randomize(0.02 * sqrt_scale); velocity.y *= 5.0; velocity.y += 0.04 * sqrt_scale; Particle * p = new CandleParticle(this, mover, coords, velocity, hue_adjust, saturation_adjust, scale, LOD); if (!base->push_back_particle(p)) break; } return true; }
bool FountainEffect::idle(const Uint64 usec) { if ((recall) && (particles.size() == 0)) return false; if (recall) return true; count += usec; while (count > 0) { const Vec3 coords = spawner->get_new_coords() + *pos + Vec3(0.0, 0.1 * sqrt_scale, 0.0); Vec3 velocity; velocity.randomize(0.2 * sqrt_scale); velocity.y = 0.0; velocity.normalize((0.15 + randfloat(0.1)) * sqrt_scale); velocity += Vec3(0.0, 1.2 * scale, 0.0); Particle * p = new FountainParticle(this, mover, coords, velocity, hue_adjust, saturation_adjust, base_height, backlight, sqrt_scale, max_size, size_scalar); if (!base->push_back_particle(p)) { count = 0; break; } count -= count_scalar * std::max(3.0f, 10.0f - LOD); } return true; }
//----------------------------------GetChromoRoulette()------------------ // // returns a chromo based on roulette wheel sampling // //----------------------------------------------------------------------- SGenome GenAlg::GetChromoRoulette() { //generate a random number between 0 & total fitness count float Slice = (float)(randfloat(0.0,1.0) * m_dTotalFitness); //this will be set to the chosen chromosome SGenome TheChosenOne; //go through the chromosones adding up the fitness so far float FitnessSoFar = 0; for (int i=0; i<m_iPopSize; ++i) { FitnessSoFar += m_vecPop[i].dFitness; //if the fitness so far > random number return the chromo at //this point if (FitnessSoFar >= Slice) { TheChosenOne = m_vecPop[i]; break; } } return TheChosenOne; }
//-------------------------------------Crossover()----------------------- // // given parents and storage for the offspring this method performs // crossover according to the GAs crossover rate //----------------------------------------------------------------------- void GenAlg::Crossover(const vector<float> &mum, const vector<float> &dad, vector<float> &baby1, vector<float> &baby2) { //just return parents as offspring dependent on the rate //or if parents are the same if ( (randfloat(0.0,1.0) > m_dCrossoverRate) || (mum == dad)) { baby1 = mum; baby2 = dad; return; } //determine a crossover point int cp = randint(0, m_iChromoLength - 1); //create the offspring for (int i=0; i<cp; ++i) { baby1.push_back(mum[i]); baby2.push_back(dad[i]); } for (int i=cp; i<mum.size(); ++i) { baby1.push_back(dad[i]); baby2.push_back(mum[i]); } return; }
void doprob(Genome* g, float p, void(*act)(Genome*)) { while (p > 0) { if (randfloat() < p) act(g); p -= 1; } }
void Cloud::gotoNewDestination() { startX = targetX; targetX = randfloat() * 280 + 180; cloudStart = SDL_GetTicks(); int distance = abs(targetX - startX); duration = distance / CLOUD_SPEED * 1000; }
Particle SphereParticleEmitter::newParticle(int anim, int time) { Particle p; float l = sys->areal.getValue(anim, time); float w = sys->areaw.getValue(anim, time); float spd = sys->speed.getValue(anim, time); float var = sys->variation.getValue(anim, time); float t = randfloat(0,2*PI); // TODO: fix shpere emitters to work properly //Vec3D bdir(l*cosf(t), 0, w*sinf(t)); Vec3D bdir(0, l*cosf(t), w*sinf(t)); /* float theta_range = sys->spread.getValue(anim, time); float theta = -0.5f* theta_range + randfloat(0, theta_range); Vec3D bdir(0, l*cosf(theta), w*sinf(theta)); float phi_range = sys->lat.getValue(anim, time); float phi = randfloat(0, phi_range); rotate(0,0, &bdir.z, &bdir.x, phi); */ p.pos = sys->pos + bdir; p.pos = sys->parent->mat * p.pos; if (bdir.lengthSquared()==0) p.speed = Vec3D(0,0,0); else { Vec3D dir = sys->parent->mrot * (bdir.normalize()); p.speed = dir.normalize() * spd * (1.0f+randfloat(-var,var)); // ? } p.down = sys->parent->mrot * Vec3D(0,-1.0f,0); p.life = 0; p.maxlife = sys->lifespan.getValue(anim, time); p.origin = p.pos; p.tile = randint(0, sys->rows*sys->cols-1); return p; }
void Population::nextgen() { int nchildren = 0; Genome** children = 0; if (nspecies > MAXSPECIES) { nspecies = MAXSPECIES; listshorten<Species*>(species, nspecies); } for (int i = 0; i < nspecies; i++) { species[i]->ngenomes /= 2; listshorten<Genome*>(species[i]->genomes, species[i]->ngenomes); int nbreed = globalfitness ? species[i]->avgfitness / globalfitness * NPOP - 1 : 1; if (species[i]->ngenomes == 0 || (nspecies > MINSPECIES && ((nbreed <= 0 && nspecies) || (species[i]->staleness > StaleLimit && i != 0)))) { delete species[i]; listremove<Species*>(species, nspecies, i); continue; } for (int j = 0; j < nbreed; j++) { Genome* child = 0; if (randfloat() < CrossoverChance) { Genome* g1 = species[i]->genomes[randint(0, species[i]->ngenomes-1)]; Genome* g2 = species[i]->genomes[randint(0, species[i]->ngenomes-1)]; child = mutate(cross(g1, g2)); } else { Genome* g1 = species[i]->genomes[randint(0, species[i]->ngenomes-1)]; child = mutate(g1); } listappend<Genome*>(children, nchildren, child); } species[i]->ngenomes = std::min(KEEPOLD, species[i]->ngenomes); listshorten<Genome*>(species[i]->genomes, species[i]->ngenomes); } if (nchildren > NPOP - KEEPOLD * nspecies) { nchildren = NPOP - KEEPOLD * nspecies; listshorten<Genome*>(children, nchildren); } addtospecies(children, nchildren); generation++; }
static void fill_random( float (* state)[NHOR][NVER] ) { for (int idy = 0; idy < NVER; idy++) { for (int idx = 0; idx < NVER; idx++) { GET(0, 0) = 2 * randfloat() - 1; } } }
void NoiseBandSynthesizer::SynthesizeSample(std::vector<double> *instantParams) { if(firstRun) { phase=0.0; phaseAdd=0.0; lastfreq=(*instantParams)[eCenterFrequency]; lastamp=(*instantParams)[eAmplitude]; lastbw=(*instantParams)[eBandwidth]; targetfreq=lastfreq; instfreq=lastfreq; samplesTillNextTarget =-1; firstRun=false; } float lowFreq,highFreq,centerFreq,maxFreq; float freq,amp,bw; maxFreq = 44100.0/2.0; freq=(*instantParams)[eCenterFrequency]; amp=(*instantParams)[eAmplitude]; bw=mymin(1.0,mymax(0.0,(*instantParams)[eBandwidth])); lowFreq = freq*(1.0-bw); highFreq = mymin(maxFreq,freq*(1.0+bw) ); centerFreq = (lowFreq+highFreq)/2.0; //see if we need a new target if(samplesTillNextTarget <0) { targetfreq = randfloat(highFreq-lowFreq)+lowFreq; //assumes bw <1.0 samplesTillNextTarget = randint(44100.0/mymax(2.0,centerFreq))+1.0; } else { samplesTillNextTarget = mymin(44100.0/mymax(2.0,centerFreq),samplesTillNextTarget); samplesTillNextTarget--; } instfreq = instfreq + (targetfreq-instfreq)/mymax(1.0,samplesTillNextTarget); phaseAdd = instfreq*2.0*3.141592/44100.0; //phaseAdd += phaseAdd* (randfloat(bw*2)-bw); phase += phaseAdd; while(phase>2.0*3.141592) phase-=2.0*3.141592; buffer->SetFrame(currentSample++,amp*sin(phase)*SHORTLIMITF); lastfreq=freq; lastamp=amp; lastbw=bw; }
// randomly initialize weights and biases //void rbm::init_paras(float ** weights, float * visible_bias, float * hidden_bias, const char * init_weights_file, const char * init_hidden_bias_file, const char * init_visible_bias_file){ void rbm::init_paras(float ** weights, float * visible_bias, float * hidden_bias){ srand(time(NULL)); std::default_random_engine generator; std::normal_distribution<double> distribution(0.0,0.01); for (int i=0; i<num_hidden_units; i++){ for (int j=0; j<num_visible_units; j++){ weights[i][j] = distribution(generator); } } // Initialize the hidden bias for (int i=0; i<num_hidden_units; i++){ hidden_bias[i] = randfloat(); } // Initialize the visible bias for (int i=0; i<num_visible_units; i++){ visible_bias[i] = randfloat(); } }
double NEAT::gaussrand() { static int iset=0; static double gset; double fac,rsq,v1,v2; if (iset==0) { do { v1=2.0*(randfloat())-1.0; v2=2.0*(randfloat())-1.0; rsq=v1*v1+v2*v2; } while (rsq>=1.0 || rsq==0.0); fac=sqrt(-2.0*log(rsq)/rsq); gset=v1*fac; iset=1; return v2*fac; } else { iset=0; return gset; } }
bool MineParticleFire::idle(const Uint64 delta_t) { const interval_t float_time = delta_t / 1000000.0; const Uint64 age = get_time() - born; const float age_f = (float)(age)/1000000.0f; const alpha_t scalar = std::pow(randfloat(), float_time * 2); if (age_f > 1.5) alpha = 2.5 - age_f; if (alpha < 0.01) return false; velocity *= scalar; return true; }
bool MissileEffect::idle(const Uint64 usec) { if (particles.size() == 0) return false; const interval_t dist = (old_pos - *pos).magnitude(); const Vec3 direction = (old_pos - *pos).normalize(0.75); if (dist < 1E-4) return true; // do not add more particles, dist < 0.0001 for (float step = 0.0; step < dist; step += (0.1 / ((1.0 + (float)hitOrMiss) / 1.5))) { const percent_t percent = step / dist; Vec3 randshift; randshift.randomize(0.025 * (1.0 + (float)hitOrMiss / 2.0)); // random particle position change const Vec3 coords = (old_pos * percent) + (*pos * (1.0 - percent)) + randshift; Vec3 velocity; velocity.randomize(0.0625 * (1.0 + (float)hitOrMiss / 2.0)); // random particle movement velocity -= direction; // follow the missile Particle* p; if (type == MAGIC && randfloat() < 0.25) { p = new MissileParticle(this, mover, coords, velocity, size / 2 + randfloat (size / 2), 1.0, randcolor(), randcolor(), randcolor(), texture, LOD, type); base->push_back_particle(p); } p = new MissileParticle(this, mover, coords, velocity, size / 2 + randfloat (size / 2), alpha / 2 + randfloat(alpha / 2), color[0], color[1], color[2], texture, LOD, type); base->push_back_particle(p); } old_pos = *pos; return true; }
int simulated_annealing(int n, double seconds) { default_random_engine rng; uniform_real_distribution<double> randfloat(0.0, 1.0); uniform_int_distribution<int> randint(0, n - 2); // random initial solution vi sol(n); rep(i,0,n) sol[i] = i + 1; random_shuffle(sol.begin(), sol.end()); // initialize score int score = 0; rep(i,1,n) score += abs(sol[i] - sol[i-1]); int iters = 0; double T0 = 100.0, T1 = 0.001, progress = 0, temp = T0, starttime = curtime(); while (true) { if (!(iters & ((1 << 4) - 1))) { progress = (curtime() - starttime) / seconds; temp = T0 * pow(T1 / T0, progress); if (progress > 1.0) break; } // random mutation int a = randint(rng); // compute delta for mutation int delta = 0; if (a > 0) delta += abs(sol[a+1] - sol[a-1]) - abs(sol[a] - sol[a-1]); if (a+2 < n) delta += abs(sol[a] - sol[a+2]) - abs(sol[a+1] - sol[a+2]); // maybe apply mutation if (delta >= 0 || randfloat(rng) < exp(delta / temp)) { swap(sol[a], sol[a+1]); score += delta; // if (score >= target) return; } iters++; } return score; }
/* * Generate a filter. A filter is a random number of random boxes. */ void Ftr::generate(FtrParams *p) { _width = p->_width; _height = p->_height; int numrects = randint(p->_minNumRect, p->_maxNumRect); _rects.resize(numrects); _weights.resize(numrects); for (int k = 0; k < numrects; k++) { _weights[k] = (randfloat() * 2 - 1) / 1000; // int wt = (randfloat()*16); // _weights[k] = (float)(wt - 8); _rects[k].x = randint(0, (uint) (p->_width - 3)); _rects[k].y = randint(0, (uint) (p->_height - 3)); _rects[k].width = randint(1, (p->_width - _rects[k].x - 2)); _rects[k].height = randint(1, (p->_height - _rects[k].y - 2)); } }
bool MineParticleSmoke::idle(const Uint64 delta_t) { if (state == 0) { const Uint64 age = get_time() - born; const float age_f = (float)(age)/1000000.0f; alpha = age_f * 0.125; if (age_f > 0.25) { state = 1; alpha = randalpha(0.375); } return true; } const interval_t float_time = delta_t / 1000000.0; const alpha_t scalar = std::pow(randfloat(), float_time); alpha *= std::sqrt(scalar); if (alpha < 0.01) return false; return true; }
TeleporterParticle::TeleporterParticle(Effect* _effect, ParticleMover* _mover, const Vec3 _pos, const Vec3 _velocity, const color_t hue_adjust, const color_t saturation_adjust, const coord_t size_scalar) : Particle(_effect, _mover, _pos, _velocity, size_scalar * (0.5 + 1.5 * randcoord())) { color_t hue, saturation, value; hue = randcolor(1.0); saturation = randfloat(0.2); value = 0.9; hue += hue_adjust; if (hue > 1.0) hue -= 1.0; saturation = std::min(1.0f, saturation * saturation_adjust); hsv_to_rgb(hue, saturation, value, color[0], color[1], color[2]); alpha = std::min(1.0f, 5.0f / size); velocity /= size; flare_max = 1.6; flare_exp = 0.2; flare_frequency = 2.0; }