void CMagic120Cell::scramble( int numTwists ) { // If we are doing a full scramble, we load from a presaved file if we can. // This is because it was way too slow and avoid having to optimize. // Also, it was Sarah's idea! bool fullScramble = numTwists >= 1000; if( fullScramble ) { // Always clear twists for a full scramble. m_twistHistory.clear(); if( m_loader.loadScrambledFile( m_state, m_twistHistory.getAllTwists() ) ) { m_state.setScrambled( true ); puzzleChanged(); return; } assert( false ); } for( int i=0; i<numTwists; i++ ) { bool left = getRandomInt( 1 ) == 1; __int8 cell = getRandomInt( m_nCells-1 ); __int8 sticker = getRandomInt( m_nStickers-2 ) + 1; // Avoids 0 sticker. m_currentTwist = STwist( getStickerHash( cell, sticker ), left ); // Apply the twist. m_cells[m_currentTwist.m_cell].twist( m_currentTwist, getCurrentTwistMagnitude(), m_settings, this, true, true, m_state, m_currentViewRotation.m ); m_twistHistory.update( m_currentTwist ); } // Cache the full scramble in case it disappeared for some reason. if( fullScramble ) { assert( false ); m_state.setScrambled( true ); m_loader.saveScrambledFile( m_state, m_twistHistory.getAllTwists() ); } // We do this so everything will be redrawn. // XXX - better name. settingsChanged(); // Reset the fading information. for( int c=0; c<m_nCells; c++ ) m_cells[c].m_changingFace = -1; }
void DirectoryPlayer::changeMusic(MUSICTYPE musicType) { int musicNum = -1; std::string filename = ""; if(currentMusicType == musicType) { return; } if(musicType >= 0 && musicType < MUSIC_NUM_MUSIC_TYPES) { musicNum = getRandomInt(0, MusicFileList[musicType].size()-1); filename = MusicFileList[musicType][musicNum]; currentMusicType = musicType; } else { // MUSIC_RANDOM int maxnum = MusicFileList[MUSIC_ATTACK].size() + MusicFileList[MUSIC_PEACE].size(); if(maxnum > 0) { unsigned int randnum = getRandomInt(0, maxnum-1); if(randnum < MusicFileList[MUSIC_ATTACK].size()) { musicNum = randnum; filename = MusicFileList[MUSIC_ATTACK][musicNum]; currentMusicType = MUSIC_ATTACK; } else { musicNum = randnum - MusicFileList[MUSIC_ATTACK].size(); filename = MusicFileList[MUSIC_PEACE][musicNum]; currentMusicType = MUSIC_PEACE; } } } if((musicOn == true) && (filename != "")) { Mix_HaltMusic(); if(music != NULL) { Mix_FreeMusic(music); music = NULL; } music = Mix_LoadMUS(filename.c_str()); if(music != NULL) { printf("Now playing %s!\n",filename.c_str()); Mix_PlayMusic(music, -1); } else { printf("Unable to play %s!\n",filename.c_str()); } } }
std::set<long> getKRandom(long max, long k) { if (max<k) throw OperationNotSupportedException("Only " + to_string(max) + " values available, requested " + to_string(k)); std::set<long> res; while (res.size()<k) res.insert(getRandomInt(max)); return res; }
inline s32 getIntFromValueRange( const CTreeGenerator::SValueRange<s32>& range, s32 seed ) { if ( range.IsValue ) return range.Value; return getRandomInt( seed, range.Min, range.Max ); }
//mutate a gType randomly using exclusive or gType mutate(gType c1){ for(int i = 0; i < c1.size(); i++){ bool t = c1[i]; t ^= getRandomInt(0,2); c1.set(i,t); } return c1; }
//pick a cross-over point to choose parental gene sequences to insert into child genome gType crossOver(gType p1, gType p2){ gType child = 0; int split = getRandomInt(0,p1.size()); std::vector<gType> genePool; genePool.push_back(p1); genePool.push_back(p2); //randomly pick the first parent sequence int n = getRandomInt(0,2); for(int i = 0; i < p1.size()-split; i++){ child.set(i, genePool[n][i]); } n+=1; n%=2; for(int i = split; i < p1.size(); i++){ child.set(i, genePool[n][i]); } return child; }
// This is needed because the hand giving to each player with initializeGame // is not very varied in card type void prepareHand(struct gameState *state, int player){ int totalCards = 27, prevCard = 0, i = 0; state->handCount[player] = 5; // Put a few treasure cards in the other player's hand state->hand[player][0] = copper; state->hand[player][1] = copper; state->hand[player][2] = silver; state->hand[player][3] = gold; state->hand[player][4] = getRandomInt(0, totalCards - 1, -2); }
void Floor::addSpriteBoundaries(sf::Sprite *sprite, int xmin, int xmax, int y) { // randomly add sprite at a location with x between min and max // and also, none of the sprites will collide at all with each other int x; if (spriteBoundaries.empty()) { x = getRandomInt(xmin, xmax); // freely choose random x if there aren't any sprites yet sprite->setPosition(x, y); } else { bool isIntersect; do { isIntersect = false; x = getRandomInt(xmin, xmax); // generate new x until current sprite doesn't collide with any other sprite for (int i = 0; !isIntersect && i < spriteBoundaries.size(); i++) { sprite->setPosition(x, y); isIntersect = sprite->getGlobalBounds().intersects(spriteBoundaries[i]->getGlobalBounds()); } } while(isIntersect); } spriteBoundaries.push_back(sprite); }
//randomly pick a parent to fulfill genetic requirements at each increment in the genome gType getRandomChild(gType p1, gType p2){ gType child; child.set(); std::vector<gType> genePool; genePool.push_back(p1); genePool.push_back(p2); for(int i = 0; i < p1.size(); i++){ int n = getRandomInt(0,2); child.set(i, genePool[n][i]); } return child; }
Firm::Firm(int avg_starting_capital, int starting_individuals) { id = GenerateID(); //bitset of all 0s gType p1 = 0; //bitset of all 1s: (2^64)-1 gType p2; p2.set(); //get random bits from each parent to create the child companyProduct = getRandomChild(p1,p2); rawProduct = getRandomChild(p1,p2); capital = getRandomInt(1, 2 * avg_starting_capital); unitsLeft = 100; timeUntraded = 0; for(int i = 0 ; i < starting_individuals; i++) { employees.push_back(new Individual()); } }
static int runClientThread(void *args) #endif { sqlite3 *sqlite; int ntrans = n_txn_per_client; if (sqlite3_open(dbname, &sqlite) != SQLITE_OK) { return 0; } sqlite3_busy_timeout(sqlite, 100000); while (ntrans-- > 0) { int account = getRandomID(ACCOUNT); int branch = getRandomID(BRANCH); int teller = getRandomID(TELLER); int delta = getRandomInt(0,1000); doOne(sqlite, branch, teller, account, delta); incrementTransactionCount(); } sqlite3_close(sqlite); return 0; }
int getFileCachePath( const char *inPath, char *cachePath ) { char myDir[MAX_NAME_LEN], myFile[MAX_NAME_LEN]; struct stat statbuf; if ( inPath == NULL || cachePath == NULL ) { rodsLog( LOG_ERROR, "getFileCachePath: input inPath or cachePath is NULL" ); return SYS_INTERNAL_NULL_INPUT_ERR; } splitPathByKey( ( char * ) inPath, myDir, MAX_NAME_LEN, myFile, MAX_NAME_LEN, '/' ); while ( 1 ) { snprintf( cachePath, MAX_NAME_LEN, "%s/%s.%u", FuseCacheDir, myFile, getRandomInt() ); if ( stat( cachePath, &statbuf ) < 0 ) { break; } } return 0; }
static int getRandomID(int type) { int min, max, num; max = min = 0; num = naccounts; switch(type) { case TELLER: min += nbranches; num = ntellers; /* FALLTHROUGH */ case BRANCH: if (type == BRANCH) { num = nbranches; } min += naccounts; /* FALLTHROUGH */ case ACCOUNT: max = min + num - 1; } return (getRandomInt(min, max)); }
float ParticleSystem::getRandomFloat(float min, float max) { return getRandomInt(min * 100.0f, max * 100.0f) / 100.0f; }
void queueSet_endlessIOLoop(queueSet_t self){ while (1){ if (_kbhit()){ char c = _getch(); if (c == VK_ESCAPE) { queueSet_delete(self); break; } } int queueNum = rand() % 2 + 1; queue_t queue = (queueNum == 1) ? self->queue1 : self->queue2; queue_t anotherQueue = (queueNum == 1) ? self->queue2 : self->queue1; int num = getRandomInt(); if (num > 0){ queue_add(queue, num); for (int i = 0; i < self->numOfSubscribers; i++){ if (self->subscribers[i]->addPrint) self->subscribers[i]->addPrint(self->subscribers[i], queueNum, num); } if (queue_getSize(queue) > 10){ if (queue_getSize(anotherQueue) < 9){ Sleep(30); int prevElem = queue_remove(queue); queue_add(anotherQueue, prevElem); for (int i = 0; i < self->numOfSubscribers; i++){ if (self->subscribers[i]->addPrint_transfer) self->subscribers[i]->addPrint_transfer(self->subscribers[i], queueNum, prevElem); } } else{ //removing last elements int enqueueMain = queue_remove(queue); int enqueueAnother = queue_remove(anotherQueue); for (int i = 0; i < self->numOfSubscribers; i++){ if (self->subscribers[i]->fullOverflow) self->subscribers[i]->fullOverflow(self->subscribers[i], queueNum, enqueueMain, enqueueAnother); } } } } else{ int prevNum = queue_remove(queue); for (int i = 0; i < self->numOfSubscribers; i++){ if (self->subscribers[i]->removePrint) self->subscribers[i]->removePrint(self->subscribers[i], queueNum, prevNum); } if (queue_getSize(anotherQueue) >= 2){ Sleep(300); int transNum = queue_remove(anotherQueue); queue_add(queue, transNum); for (int i = 0; i < self->numOfSubscribers; i++){ if (self->subscribers[i]->removePrint_transfer) self->subscribers[i]->removePrint_transfer(self->subscribers[i], queueNum, transNum); } } else{ int num11 = abs(getRandomInt()); int num12 = abs(getRandomInt()); int num13 = abs(getRandomInt()); int num22 = abs(getRandomInt()); int num23 = abs(getRandomInt()); queue_add(queue, num11); queue_add(queue, num12); queue_add(queue, num13); queue_add(anotherQueue, num22); queue_add(anotherQueue, num23); for (int i = 0; i < self->numOfSubscribers; i++){ if (self->subscribers[i]->fullProcessing) self->subscribers[i]->fullProcessing(self->subscribers[i], queueNum, num11, num12, num13, num22, num23); } } } Sleep(300); } }
int createPhyBundleDataObj( rsComm_t *rsComm, char *collection, const std::string& _resc_name, const char* rescHier, dataObjInp_t *dataObjInp, // should be able to only use rescHier char* dataType ) { // JMC - backport 4658 int l1descInx; int status; /* XXXXXX We do bundle only with UNIX_FILE_TYPE for now */ if ( _resc_name.empty() ) { return SYS_INTERNAL_NULL_INPUT_ERR; } std::string type; irods::error err = irods::get_resource_property< std::string >( _resc_name, irods::RESOURCE_TYPE, type ); if ( !err.ok() ) { irods::log( PASS( err ) ); } do { int loopCnt = 0; bzero( dataObjInp, sizeof( dataObjInp_t ) ); while ( 1 ) { status = rsMkBundlePath( rsComm, collection, dataObjInp->objPath, getRandomInt() ); if ( status < 0 ) { rodsLog( LOG_ERROR, "createPhyBundleFile: getPhyBundlePath err for %s.stat = %d", collection, status ); return status; } /* check if BundlePath already existed */ if ( isData( rsComm, dataObjInp->objPath, NULL ) >= 0 ) { if ( loopCnt >= 100 ) { break; } else { loopCnt++; continue; } } else { break; } } if ( dataType != NULL && strstr( dataType, BUNDLE_STR ) != NULL ) { // JMC - backport 4658 addKeyVal( &dataObjInp->condInput, DATA_TYPE_KW, dataType ); } else { /* assume it is TAR_BUNDLE_DT_STR */ addKeyVal( &dataObjInp->condInput, DATA_TYPE_KW, TAR_BUNDLE_DT_STR ); } if ( rescHier != NULL ) { addKeyVal( &dataObjInp->condInput, RESC_HIER_STR_KW, rescHier ); } if ( dataType != NULL && strstr( dataType, ZIP_DT_STR ) != NULL ) { // JMC - backport 4664 /* zipFile type. must end with .zip */ int len = strlen( dataObjInp->objPath ); if ( strcmp( &dataObjInp->objPath[len - 4], ".zip" ) != 0 ) { strcat( dataObjInp->objPath, ".zip" ); } } l1descInx = _rsDataObjCreateWithResc( rsComm, dataObjInp, _resc_name ); clearKeyVal( &dataObjInp->condInput ); } while ( l1descInx == OVERWRITE_WITHOUT_FORCE_FLAG ); if ( l1descInx >= 0 ) { l3Close( rsComm, l1descInx ); L1desc[l1descInx].l3descInx = 0; if ( dataType != NULL && strstr( dataType, ZIP_DT_STR ) != NULL ) { // JMC - backport 4664 l3Unlink( rsComm, L1desc[l1descInx].dataObjInfo ); } } return l1descInx; }
void ADLPlayer::changeMusic(MUSICTYPE musicType) { int musicNum = -1; std::string filename = ""; if((currentMusicType == musicType) && (pSoundAdlibPC != NULL) && pSoundAdlibPC->isPlaying()) { return; } /* currently unused: DUNE0.ADL/4 DUNE1.ADL/2 and DUNE10.ADL/2 DUNE20.ADL/2 */ switch(musicType) { case MUSIC_ATTACK: { switch(getRandomInt(0, 5)) { case 0: filename = "DUNE10.ADL"; musicNum = 7; break; case 1: filename = "DUNE11.ADL"; musicNum = 7; break; case 2: filename = "DUNE12.ADL"; musicNum = 7; break; case 3: filename = "DUNE13.ADL"; musicNum = 7; break; case 4: filename = "DUNE14.ADL"; musicNum = 7; break; case 5: filename = "DUNE15.ADL"; musicNum = 7; break; } } break; case MUSIC_PEACE: { switch(getRandomInt(0, 7)) { case 0: filename = "DUNE2.ADL"; musicNum = 6; break; case 1: filename = "DUNE3.ADL"; musicNum = 6; break; case 2: filename = "DUNE4.ADL"; musicNum = 6; break; case 3: filename = "DUNE5.ADL"; musicNum = 6; break; case 4: filename = "DUNE6.ADL"; musicNum = 6; break; case 5: filename = "DUNE9.ADL"; musicNum = 4; break; case 6: filename = "DUNE9.ADL"; musicNum = 5; break; case 7: filename = "DUNE18.ADL"; musicNum = 6; break; } } break; case MUSIC_INTRO: { filename = "DUNE0.ADL"; musicNum = 2; } break; case MUSIC_MENU: { filename = "DUNE7.ADL"; musicNum = 6; } break; case MUSIC_BRIEFING_H: { filename = "DUNE7.ADL"; musicNum = 2; } break; case MUSIC_BRIEFING_A: { filename = "DUNE7.ADL"; musicNum = 3; } break; case MUSIC_BRIEFING_O: { filename = "DUNE7.ADL"; musicNum = 4; } break; case MUSIC_WIN_H: { filename = "DUNE8.ADL"; musicNum = 3; } break; case MUSIC_WIN_A: { filename = "DUNE8.ADL"; musicNum = 2; } break; case MUSIC_WIN_O: { filename = "DUNE17.ADL"; musicNum = 4; } break; case MUSIC_LOSE: { filename = "DUNE1.ADL"; musicNum = 4; } break; case MUSIC_GAMESTAT: { switch(getRandomInt(0, 1)) { case 0: filename = "DUNE10.ADL"; musicNum = 7; break; case 1: filename = "DUNE11.ADL"; musicNum = 7; break; } } break; case MUSIC_MAPCHOICE: { filename = "DUNE16.ADL"; musicNum = 7; } break; case MUSIC_MEANWHILE: { filename = "DUNE16.ADL"; musicNum = 8; } break; case MUSIC_FINALE_H: { filename = "DUNE19.ADL"; musicNum = 4; } break; case MUSIC_FINALE_A: { filename = "DUNE19.ADL"; musicNum = 2; } break; case MUSIC_FINALE_O: { filename = "DUNE19.ADL"; musicNum = 3; } break; case MUSIC_RANDOM: default: { switch(getRandomInt(0, 13)) { // attack case 0: filename = "DUNE10.ADL"; musicNum = 7; break; case 1: filename = "DUNE11.ADL"; musicNum = 7; break; case 2: filename = "DUNE12.ADL"; musicNum = 7; break; case 3: filename = "DUNE13.ADL"; musicNum = 7; break; case 4: filename = "DUNE14.ADL"; musicNum = 7; break; case 5: filename = "DUNE15.ADL"; musicNum = 7; break; // peace case 6: filename = "DUNE2.ADL"; musicNum = 6; break; case 7: filename = "DUNE3.ADL"; musicNum = 6; break; case 8: filename = "DUNE4.ADL"; musicNum = 6; break; case 9: filename = "DUNE5.ADL"; musicNum = 6; break; case 10: filename = "DUNE6.ADL"; musicNum = 6; break; case 11: filename = "DUNE9.ADL"; musicNum = 4; break; case 12: filename = "DUNE9.ADL"; musicNum = 5; break; case 13: filename = "DUNE18.ADL"; musicNum = 6; break; } } break; } currentMusicType = musicType; if((musicOn == true) && (filename != "")) { Mix_HookMusic(NULL, NULL); delete pSoundAdlibPC; SDL_RWops* rwop = pFileManager->openFile(filename); if(rwop == NULL) { printf("Unable to load %s!\n",filename.c_str()); } else { pSoundAdlibPC = new SoundAdlibPC(rwop); SDL_RWclose(rwop); pSoundAdlibPC->playTrack(musicNum); Mix_HookMusic(pSoundAdlibPC->callback, pSoundAdlibPC); printf("Now playing %s!\n",filename.c_str()); } } }
// create some cool map or something void Map::init() { for (int z = 0; z < 2; z++) { for (int x = 0; x < 128; x++) { for (int y = 0; y < 128; y++) { Tile *t = getMapTile(x, y, z); if (x > 10 && x < 20) { t->setDraw(true); t->setPassable(true); t->setGridX(getRandomInt(2, 3)); t->setGridY(getRandomInt(2, 4)); } } } for (int x = 0; x < 128; x++) { for (int y = 0; y < 128; y++) { Tile *t = getMapTile(x, y, z); if (y > 10 && y < 20) { t->setDraw(true); t->setPassable(true); t->setGridX(getRandomInt(2, 3)); t->setGridY(getRandomInt(2, 4)); } } } } setTileGrid(0, 10, 0, 0, 8); setTilePassable(0,10, 0, false); setTileGrid(1, 10, 0, 0, 8); setTilePassable(1,10, 0, false); setTileGrid(2, 10, 0, 0, 8); setTilePassable(2,10, 0, false); setTileGrid(0, 30, 0, 0, 8); setTilePassable(0,30, 0, false); setTileGrid(1, 30, 0, 0, 8); setTilePassable(1,30, 0, false); setTileGrid(2, 30, 0, 0, 8); setTilePassable(2,30, 0, false); // crate setTileGrid(10, 7, 0, 0, 0); setTilePassable(10,7, 0, false); setTileGrid(11, 7, 0, 0, 0); setTilePassable(11,7, 0, false); setTileGrid(10, 8, 0, 0, 0); setTilePassable(10,8, 0, false); setTileGrid(11, 8, 0, 0, 0); setTilePassable(11,8, 0, false); // crate setTileGrid(20, 13, 0, 0, 0); setTilePassable(20, 13, 0, false); setTileGrid(21, 13, 0, 0, 0); setTilePassable(20, 13, 0, false); setTileGrid(20, 14, 0, 0, 0); setTilePassable(20,14, 0, false); setTileGrid(21, 14, 0, 0, 0); setTilePassable(20,14, 0, false); }
int initAgent( int processType, rsComm_t *rsComm ) { int status; rsComm_t myComm; ruleExecInfo_t rei; initProcLog(); status = initServerInfo( rsComm ); if ( status < 0 ) { rodsLog( LOG_ERROR, "initAgent: initServerInfo error, status = %d", status ); return status; } initL1desc(); initSpecCollDesc(); status = initFileDesc(); if ( status < 0 ) { rodsLog( LOG_ERROR, "initAgent: initFileDesc error, status = %d", status ); return status; } #ifdef TAR_STRUCT_FILE // initStructFileDesc (); // initTarSubFileDesc (); #endif status = initRuleEngine( processType, rsComm, reRuleStr, reVariableMapStr, reFuncMapStr ); if ( status < 0 ) { rodsLog( LOG_ERROR, "initAgent: initRuleEngine error, status = %d", status ); return status; } memset( &rei, 0, sizeof( rei ) ); rei.rsComm = rsComm; if ( ProcessType == AGENT_PT ) { status = applyRule( "acChkHostAccessControl", NULL, &rei, NO_SAVE_REI ); if ( status < 0 ) { rodsLog( LOG_ERROR, "initAgent: acChkHostAccessControl error, status = %d", status ); return status; } } /* Initialize the global quota */ GlobalQuotaLimit = RESC_QUOTA_UNINIT; GlobalQuotaOverrun = 0; RescQuotaPolicy = RESC_QUOTA_UNINIT; #ifndef windows_platform if ( rsComm->reconnFlag == RECONN_TIMEOUT ) { rsComm->reconnSock = svrSockOpenForInConn( rsComm, &rsComm->reconnPort, &rsComm->reconnAddr, SOCK_STREAM ); if ( rsComm->reconnSock < 0 ) { rsComm->reconnPort = 0; rsComm->reconnAddr = NULL; } else { rsComm->cookie = ( int )( getRandomInt() >> 1 ); } try { rsComm->thread_ctx->lock = new boost::mutex; rsComm->thread_ctx->cond = new boost::condition_variable; rsComm->thread_ctx->reconnThr = new boost::thread( reconnManager, rsComm ); } catch ( boost::thread_resource_error& ) { rodsLog( LOG_ERROR, "boost encountered thread_resource_error." ); return SYS_THREAD_RESOURCE_ERR; } }
void BusinessCycle::cycle() { /* firms create If a firm realizes there is no product within a suitable modicum of acceptance, they may invest a certain % of their capital to create a new firm The mother firm may take starting individuals from the unemployment pool to seed the child firm’s ranks */ for(int i = 0; i < firmindex.size(); i++) { //this firm Firm* firm = firmindex.at(i); double productivity = firm->getproductivity(); firm->timeUntraded++; //get a list of employees with which to work std::vector<Individual*> empls = firm->getemployees(); //hire fire threshold double hft = (double)config->get_hire_fire_threshold()/100.0; //hire fire rate int hfr = (int)((double)empls.size() * hft); if(hfr = 0) hfr = 2; //best product for this firm Firm* hasBestProduct = firmindex.at(0); while(hasBestProduct->id == firm->id) hasBestProduct = firmindex.at(getRandomInt(0,firmindex.size())); //how close the product is to what is required double bestDist = 1.0; //find the best product for this firm for(int j = 0; j < firmindex.size(); j++) { if(firmindex.at(j)->unitsLeft > 0 && firmindex.at(j)->id != firm->id) { gType tempProd = firmindex.at(j)->companyProduct; double dist = (double)getHammingDistance(tempProd, firm->rawProduct)/(double)tempProd.size(); if(dist < bestDist) { bestDist = dist; hasBestProduct = firmindex.at(j); } } } double q = (double)(100 - config->get_modicum_of_acceptance())/100.0; if(config->get_avg_starting_capital()/2 < firm->capital && bestDist < q) { double starting = firm->capital / 2; firm->capital -= starting; gType prod = firm->rawProduct; std::vector<Individual*> startingPpl; Individual* tempEmp; for(int c = 0; c < unemployed.size(); c++) { if((double)unemployed.at(c)->getproductivity(prod) > 1.0-hft/2) { tempEmp = unemployed.at(c); startingPpl.push_back(tempEmp); unemployed.erase(unemployed.begin() + c); } else c++; } Firm* newFirm = new Firm(0, config->get_start_individuals()); newFirm->unitsLeft = 100; newFirm->companyProduct = prod; newFirm->capital = starting; firmindex.push_back(newFirm); } //how much does the product cost?: (quality of raw) firm->productCost = 1.0;//-bestDist; //while there is product left and the firm has enough money to purchase it int raw = 0; //std::cout << "\n---Firm "<<firm->id << "---\n" <<"\nRaw units available: " << hasBestProduct->unitsLeft << "\n"; double before = firm->capital; while(hasBestProduct->unitsLeft > 0 && firm->capital >= hasBestProduct->productCost) { //make it so hasBestProduct->unitsLeft--; hasBestProduct->capital += hasBestProduct->productCost; firm->capital -= hasBestProduct->productCost; raw++; } firm->buysFrom = hasBestProduct; double after = firm->capital; //std::cout << "Raw units acquired: " << raw << " from Firm: "<< hasBestProduct->id <<"\n"; //how much product is produced?: (int)(productivity)(qty of raw purchased)*(#employees) firm->unitsLeft += (int)(((double)productivity+.5)*(double)raw); //std::cout << "Firm " << firm->id << " created " << (int)((double)productivity*(double)raw) << " units.\n"; //std::cout << "Firm " << firm->id << " has " << firm->unitsLeft << " units that will sell for " << firm->productCost <<" each.\n"; //std::cout << "Our cost was: " << getDelta(before,after) << ". \n"; hasBestProduct->timeUntraded = 0; //money += qtypurchased*(quality of raw) //individuals mentor //declare the product for each employee so we can sort based on productivity firm->employeeProductUpdate(); std::vector<Individual*> mentors; // get the number of people who will be mentoring int numMentors = (int)(empls.size()*(double)config->get_modicum_of_acceptance()/100.0); //std::cout << firmindex.at(i)->getemployees().size()<<"\n"; firm->sortEmployees(); //get the mentors for(int j = 0; j < numMentors; j++) { //a certain percentage of individuals within the firm whose skill sets match the product type most closely //must mentor new individuals who start at an initial age if(!empls.at(j)->isRetired()) { mentors.push_back(empls.at(j)); } } // mentor new employee for each mentor pair for(int j = 1; j < mentors.size(); j++) { Individual* x; //They may do this via random skill (bit) selection or via crossover if(getRandomInt(0,2)) x = new Individual(mentors.at(j)->skillSet, mentors.at(j-1)->skillSet, false); else { x = new Individual(mentors.at(j)->skillSet, mentors.at(j-1)->skillSet, true); } //"retrain" the least effective employee empls.at(empls.size()-1)->skillSet = mutate(empls.at(empls.size()-1)->skillSet); firm->employees.push_back(x); all_the_peoples.push_back(x); } Individual* tempEmp; int numFired = 0; //firms may fire individuals for(int c = 0; c < empls.size();) { double empProductivity = empls.at(c)->getproductivity(firm->companyProduct); if(getDelta(empProductivity,productivity) > hft) { tempEmp = empls.at(c); empls.erase(empls.begin()+c); unemployed.push_back(tempEmp); numFired++; } else c++; } // firms may hire individuals numFired = getRandomInt(0, numFired+1); for(int c = 0; c < unemployed.size() && numFired >0;) { double empProductivity = empls.at(c)->getproductivity(firm->companyProduct); if(getDelta(empProductivity,productivity) <= hft) { tempEmp = unemployed.at(c); unemployed.erase(unemployed.begin()+c); empls.push_back(tempEmp); numFired--; } else c++; } //if they are retired, turn them into soylent green. for(int c = 0; c < empls.size();) { if(empls.at(c)->isRetired()) { for(int y = 0; y < all_the_peoples.size(); y++) { if(empls.at(c) == all_the_peoples.at(y)) { all_the_peoples.erase(all_the_peoples.begin()+y); break; } } empls.erase(empls.begin()+c); } else c++; } if(empls.size() ==0 || firm->timeUntraded > config->get_inactivity_rate()) { // lay everyone off if there is anyone there while(!empls.empty()) { Individual* temp = empls.back(); empls.pop_back(); unemployed.push_back(temp); } // remove the firm forever. firmindex.erase(firmindex.begin()+i); i--; } } //get all employees for(int j = 0; j < all_the_peoples.size(); j++) { //iterate their ages all_the_peoples.at(j)->age++; } for(int j = 0; j < unemployed.size(); j++) { //iterate their ages unemployed.at(j)->timeUnemployed++; } //if they have given up, turn them into soylent green. for(int c = 0; c < unemployed.size();) { if(unemployed.at(c)->timeUnemployed > config->get_inactivity_rate()) { for(int y = 0; y < all_the_peoples.size(); y++) { if(unemployed.at(c) == all_the_peoples.at(y)) { all_the_peoples.erase(all_the_peoples.begin()+y); break; } } unemployed.erase(unemployed.begin()+c); } else c++; } }
void XMIPlayer::changeMusic(MUSICTYPE musicType) { int musicNum = -1; std::string filename = ""; if(currentMusicType == musicType && Mix_PlayingMusic()) { return; } /* currently unused: DUNE0.XMI/4 DUNE1.XMI/2 and DUNE10.XMI/2 */ switch(musicType) { case MUSIC_ATTACK: { switch(getRandomInt(0, 5)) { case 0: filename = "DUNE10.XMI"; musicNum = 7; break; case 1: filename = "DUNE11.XMI"; musicNum = 7; break; case 2: filename = "DUNE12.XMI"; musicNum = 7; break; case 3: filename = "DUNE13.XMI"; musicNum = 7; break; case 4: filename = "DUNE14.XMI"; musicNum = 7; break; case 5: filename = "DUNE15.XMI"; musicNum = 7; break; } } break; case MUSIC_PEACE: { switch(getRandomInt(0, 8)) { case 0: filename = "DUNE1.XMI"; musicNum = 6; break; case 1: filename = "DUNE2.XMI"; musicNum = 6; break; case 2: filename = "DUNE3.XMI"; musicNum = 6; break; case 3: filename = "DUNE4.XMI"; musicNum = 6; break; case 4: filename = "DUNE5.XMI"; musicNum = 6; break; case 5: filename = "DUNE6.XMI"; musicNum = 6; break; case 6: filename = "DUNE9.XMI"; musicNum = 4; break; case 7: filename = "DUNE9.XMI"; musicNum = 5; break; case 8: filename = "DUNE18.XMI"; musicNum = 6; break; } } break; case MUSIC_INTRO: { filename = "DUNE0.XMI"; musicNum = 2; } break; case MUSIC_MENU: { filename = "DUNE7.XMI"; musicNum = 6; } break; case MUSIC_BRIEFING_H: { filename = "DUNE7.XMI"; musicNum = 2; } break; case MUSIC_BRIEFING_A: { filename = "DUNE7.XMI"; musicNum = 3; } break; case MUSIC_BRIEFING_O: { filename = "DUNE7.XMI"; musicNum = 4; } break; case MUSIC_WIN_H: { filename = "DUNE8.XMI"; musicNum = 3; } break; case MUSIC_WIN_A: { filename = "DUNE8.XMI"; musicNum = 2; } break; case MUSIC_WIN_O: { filename = "DUNE17.XMI"; musicNum = 4; } break; case MUSIC_LOSE: { filename = "DUNE1.XMI"; musicNum = 4; } break; case MUSIC_GAMESTAT: { filename = "DUNE20.XMI"; musicNum = 2; } break; case MUSIC_MAPCHOICE: { filename = "DUNE16.XMI"; musicNum = 7; } break; case MUSIC_MEANWHILE: { filename = "DUNE16.XMI"; musicNum = 8; } break; case MUSIC_FINALE_H: { filename = "DUNE19.XMI"; musicNum = 4; } break; case MUSIC_FINALE_A: { filename = "DUNE19.XMI"; musicNum = 2; } break; case MUSIC_FINALE_O: { filename = "DUNE19.XMI"; musicNum = 3; } break; case MUSIC_RANDOM: default: { switch(getRandomInt(0, 14)) { // attack case 0: filename = "DUNE10.XMI"; musicNum = 7; break; case 1: filename = "DUNE11.XMI"; musicNum = 7; break; case 2: filename = "DUNE12.XMI"; musicNum = 7; break; case 3: filename = "DUNE13.XMI"; musicNum = 7; break; case 4: filename = "DUNE14.XMI"; musicNum = 7; break; case 5: filename = "DUNE15.XMI"; musicNum = 7; break; // peace case 6: filename = "DUNE1.XMI"; musicNum = 6; break; case 7: filename = "DUNE2.XMI"; musicNum = 6; break; case 8: filename = "DUNE3.XMI"; musicNum = 6; break; case 9: filename = "DUNE4.XMI"; musicNum = 6; break; case 10: filename = "DUNE5.XMI"; musicNum = 6; break; case 11: filename = "DUNE6.XMI"; musicNum = 6; break; case 12: filename = "DUNE9.XMI"; musicNum = 4; break; case 13: filename = "DUNE9.XMI"; musicNum = 5; break; case 14: filename = "DUNE18.XMI"; musicNum = 6; break; } } break; } currentMusicType = musicType; if((musicOn == true) && (filename != "")) { SDL_RWops* inputrwop = pFileManager->openFile(filename); if(inputrwop == NULL) { std::cerr << "Cannot open file " << filename << "!" << std::endl; return; } SDLDataSource input(inputrwop,1); std::string tmpFilename = getTmpFileName(); SDL_RWops* outputrwop = SDL_RWFromFile(tmpFilename.c_str(),"wb"); if(outputrwop == NULL) { std::cerr << "Cannot open file " << tmpFilename << "!" << std::endl; return; } SDLDataSource output(outputrwop,1); XMIDI myXMIDI(&input, XMIDI_CONVERT_NOCONVERSION); myXMIDI.retrieve(musicNum, &output ); input.close(); output.close(); Mix_HaltMusic(); if(music != NULL) { Mix_FreeMusic(music); music = NULL; } music = Mix_LoadMUS(tmpFilename.c_str()); if(music != NULL) { printf("Now playing %s!\n", tmpFilename.c_str()); Mix_PlayMusic(music, -1); } else { printf("Unable to play %s: %s!\n", filename.c_str(), Mix_GetError()); } } }
void getRandomPrime(mpz_class& z, size_t bitLen, RG& rg) { do { getRandomInt(z, bitLen, rg); } while (!Gmp::isPrime(z)); }
void queueSet_initialize(queueSet_t self){ for (int i = 0; i < 3; i++){ queue_add(self->queue1, abs(getRandomInt())); queue_add(self->queue2, abs(getRandomInt())); } }
bool Simulation::setup(Configurator* configuration){ //initialize random number function srand(time(NULL)); // number of firms int firmcounter = 0; // pointer to an individual Individual* indiepointer; // iterator over a vector of individual pointers std::vector<Individual*>::iterator it; // vector of individual pointers std::vector<Individual*> addworkers; std::cout << "Starting Simulation initialization" << std::endl; // while the counter is less than the number of starting firms multiplied by the number of starting individuals, iterate for(int counter = 0; counter < (configuration->get_startfirms() * configuration->get_start_individuals()); counter++) { // add a new individual to the workforce workForce.push_back(new Individual()); } int upperbnd = getRandomInt(1, configuration->get_startfirms()); // while the counter is less than a random number between 1 and the starting number of firms, iterate for(int counter = 0; counter < upperbnd; counter++) { // add a new individual to the workforce workForce.push_back(new Individual()); } std::cout << "Initialized workforce" << std::endl; // iterating integers int iter = 0; int iter2 = 0; // for each firm while(firmcounter < configuration->get_startfirms()) { // for each individual while(iter2 < configuration->get_start_individuals()) { addworkers.push_back(workForce.at(iter)); iter2++; iter++; } //reset iterator 2 to zero iter2 = 0; // push a new firm into the list of firms with a list of workers "addworkers" allFirms.push_back(new Firm(getRandomInt(0, configuration->get_avg_starting_capital() * 2), addworkers)); // push that firms product into the market product list productlist.push_back(allFirms.back()->getcompanyProduct()); std::cout << "Firm " << firmcounter + 1 << " initialization complete, productivity: " << allFirms.back()->getproductivity() << std::endl; // iterate firmcounter firmcounter++; // clear the addworkers vector addworkers.clear(); } std::cout << "initialized all firms" << std::endl; // for everyone else; they are unemployed for the first cycle while(iter < workForce.size()) { unemployedpeeps.push_back(workForce.at(iter)); iter++; } std::cout << "Initialized the unemployed" << std::endl; }