term_t cbif_rand_bytes1(proc_t *proc, term_t *regs) { term_t N = regs[0]; if (!is_int(N) || int_value(N) < 0) badarg(N); int len = int_value(N); uint8_t *ptr; term_t bin = heap_make_bin(&proc->hp, len, &ptr); uint8_t *p = ptr; while (p <= ptr +len -4) { uint32_t rnd = mt_lrand(); PUT_UINT_32(p, rnd); p += 4; } uint32_t last = mt_lrand(); switch(ptr +len -p) { case 3: *p++ = (uint8_t)(last >> 16); case 2: *p++ = (uint8_t)(last >> 8); case 1: *p++ = (uint8_t)last; case 0: break; } return bin; }
cLight::cLight() { light = loadImage("Media/Images/light.png"); crash = Mix_LoadWAV("Media/Sounds/crash.wav"); lightswitch = Mix_LoadWAV("Media/Sounds/lightswitch.wav"); absTime = SDL_GetTicks(); animTime = SDL_GetTicks(); state = STATE_MOVINGRIGHT; delay = mt_lrand() % 15000 + 15000; }
int cField::placeFurns(int rcb, int amount) { std::list<cCell*> cells; // Clean the field. Being clean's never a bad thing for (int y = 0; y < 9; y++) { for (int x = 0; x < 15; x++) { field[x][y]->setFurn(false, NULL); field[x][y]->dVis = false; } } // Make a list of all the possible cells to put furniture // Keep in mind the starting position, ending position & first move (rcb = right corner bottom) for (int y = 0; y < 9; y++) { for (int x = 0; x < 15; x++) { if (!(rcb == POS_R && x == 1 && y == 0) != !(rcb == POS_C && x == 1 && y == 1) != !(rcb == POS_B && x == 0 && y == 1)) { if (!((x == 0 && y == 0) || (x == 14 && y == 8))) { cells.push_back(field[x][y]); } } } } // Place diff amount of pieces of furniture in the room int c; std::list<cCell*>::iterator which; for (int i = 0; i < amount; ++i) { c = mt_lrand() % cells.size(); which = cells.begin(); for (int count = 0; count < c; ++count) { ++which; } (*which)->setFurn(true, furnies[mt_lrand() % 9]); cells.erase(which); } return 0; }
dvec *DiffEvolution::train() { uint size = _pop->size(); uint dsize = _pop->at(0)->size(); dvec *scores = new dvec(size); double min = 1e100; double minPos = 0; double cr = 0.9; double f = 0.8; if(size < 4) return NULL; //evaluation for(uint i = 0 ; i < size ; i++) { scores->at(i) = _ff->run(_pop->at(i)); if(_verbose) { std::cout << "Settler " << i << " ( " << scores->at(i) << " ) "; if(_verbose == 2) std::cout << *(_pop->at(i)); std::cout << std::endl; } } for(int g = 0 ; g < _ng ; g++) { std::cout << "Generation " << g << std::endl; //combination and mutation #pragma omp parallel for for(uint i = 0 ; i < size ; i++) { uint jmin = mt_lrand() % dsize; uint r1 = mt_lrand() % size; uint r2 = mt_lrand() % size; uint r3 = mt_lrand() % size; while (r1 == i) r1 = mt_lrand() % size; while (r2 == r1 || r2 == i) r2 = mt_lrand() % size; while (r3 == r1 || r3 == r1 || r3 == i) r3 = mt_lrand() % size; for(uint j = 0 ; j < dsize ; j++) { if (j == jmin || mt_drand() < cr) { double ng = _pop->at(r1)->at(j) + f * (_pop->at(r2)->at(j)-_pop->at(r3)->at(j)); _next->at(i)->at(j) = ng; } else { _next->at(i)->at(j) = _pop->at(i)->at(j); } } double childScore = _ff->run(_next->at(i)); #pragma omp critical { if( childScore < scores->at(i)) { _pop->at(i)->copy(*(_next->at(i))); scores->at(i) = childScore; if(_verbose == 2) { std::cout << "Changed element " << i << " ( " << scores->at(i) << " ): " << *(_pop->at(i)) << std::endl; } if(childScore < min) { min = childScore; minPos = i; if(_verbose) std::cout << "New best: " << min << std::endl; } } } } } delete scores; dvec *result = new dvec(dsize); result->copy(*(_pop->at(minPos))); return result; }
/* Uses program-wide state. */ long random_mt() { long value; // get unsigned value and convert to positive signed by zeroing the top bit value = mt_lrand() & 0x7FFFFFFF; return value; }