void startCentipede(Centipede centipedes[], int *bodyCount) { int direction, x, i; direction = LEFT + random2(1); x = random2(30) + 1; // 1-31, so starts minimum of one from edge if(direction == RIGHT) x += 5; // guarantee room for body to left of mouth for(i = 0; i < 6; i++) { centipedes[i].direction = direction; centipedes[i].icon = BODY; centipedes[i].y = TOP; if(direction == LEFT) centipedes[i].x = x + i; else // moving RIGHT centipedes[i].x = x - i; centipedes[i].icon_below = crt[TOP][centipedes[i].x]; putChar(centipedes[i].icon, centipedes[i].x, TOP); } // for each centipede body part if(direction == RIGHT) centipedes[0].icon = '{'; else centipedes[0].icon = '}'; putChar(centipedes[0].icon, centipedes[0].x, TOP); *bodyCount = 6; } // startCentipede()
void startTetromino(Tetromino *tetromino) { tetromino->location = random2(5) + 1; tetromino->pattern = random2(6); tetromino->rotation = 0; printTetromino(tetromino, 'X'); } // startTetromino()
int8_t freeOnePlace(uint8_t room,uint8_t row,uint8_t column) { if (roomOutOfBound(room)) { room=random2(MAX_HALLS); printf("room=%" PRIu8,room); } if(rowOutOfBound(row)) { row=random2(MAX_ROWS); printf("row=%" PRIu8,row); } if(columnOutOfBound(column)) { column=random2(MAX_COLUMNS); printf("column=%" PRIu8,column); } if (hall[room].chair[row][column]==FREE) { return 1; } else { hall[room].chair[row][column]=FREE; return 0; } }
double dexprand() /* Generates a double exponentially distributed random variable with mean 0 and variance 2. */ { double a,random2(); a= -log(random2()); if( random2()>0.5 ) a=(-a); return(a); }
void Controller::randomizeSettings() { auto& settings = m_currDrawer->settings(); auto& settingsRanges = m_currDrawer->settingsRanges(); for (auto& setting: settings) { auto& range = settingsRanges.find(setting.first)->second; setting.second = random2() % (range.second - range.first + 1) + range.first; } m_currPalIndex = random2() % m_palettes.size(); m_currDrawer->reset(); m_drawerChangeTimer.reset(); }
void Lic::doPixelImage(int width, int height, int pix, coDoPixelImage **pixels) { long seed = -37; int PIXEL_SIZE = pix; int PIXEL_FORMAT = pix; int num_pixels = (width) * (height); int img_size = PIXEL_SIZE * num_pixels; char *image = new char[img_size]; { for (int i = 0; i < num_pixels; i++) { if (PIXEL_SIZE == 1) { image[i] = static_cast<char>(255.0 * random2(&seed) + 0.5); if (image[i] < 128) { image[i] = 0; } else { image[i] = 255; } } else if (PIXEL_SIZE == 4) { image[4 * i] = static_cast<unsigned char>(255.0 * random2(&seed) + 0.5); image[4 * i + 1] = static_cast<unsigned char>(255.0 * random2(&seed) + 0.5); image[4 * i + 2] = static_cast<unsigned char>(255.0 * random2(&seed) + 0.5); image[4 * i + 3] = 255; } else { Covise::sendError("\"Unimplemented\" Pixel Size"); return; } } } char *name = new char[strlen(textureOutPort->getObjName()) + 5]; strcpy(name, textureOutPort->getObjName()); strcat(name, "_Img"); *pixels = new coDoPixelImage(name, width, height, PIXEL_FORMAT, PIXEL_SIZE, image); return; }
int main() { int answer, count=5; int a, b, n; int goodAnswers=0, badAnswers=0; while( count-- ){ a=random(); b=random2(); printf("%i * %i = ", a, b); getResult(&answer); n = checkAnswer(a, b, answer); giveFeedback(n, a, b); if( n==1 ) goodAnswers +=1; else badAnswers +=1; } printf("\nYou got %i answer(s) correctly and %i answer(s) incorrectly!\n\n", goodAnswers, badAnswers); return 0; }
double normal() /* Generates normal random numbers: N(0,1) */ { static int32_t even = 1; /* if even = 0: return b */ /* even = 1: compute 2 new values */ static double b; /* temporary storage */ double a,r,theta,random2(); if((even=!even)) { return(b); } else { theta=2*PI*random2(); r=sqrt(-2*log(random2())); a=r*cos(theta); b=r*sin(theta); return(a); } }
///////////////////////////////////////////////////////// // random // ///////////////////////////////////////////////////////// void newWave :: noise(float rnd) { int i, j; for (i=0; i<gridX; i++) for ( j=0;j<gridY; j++) { force[i][j] += rnd * (double)random2() * (1. / 2147483648.) - rnd/2; } }
void startNewScreen(Centipede centipedes[], int counts[14][38], int sparkXs[14], int score, int wandX, int *bodyCount) { int i, j, direction, x, y; char str[10]; erase(); // clear screen completely for(x = 0; x < 38; x++) for(y = 0; y < 14; y++) { crt[y][x] = ' '; counts[y][x] = 0; } // for y mvaddstr(0,0,"----------------------------------------"); for(y = 1; y < 15; y++) mvaddstr(y, 0, "| |"); mvaddstr(15,0,"----------------------------------------"); mvaddstr(16,0,"Borders are not in CUSP"); showScore(score); for(i = 0; i < 14; i++) sparkXs[i] = NO_SPARK; // no spark at each row startCentipede(centipedes, bodyCount); for(i = 0; i < 20; i++) { y = random2(11) + 1; x = random2(37); random2(5); // to avoid having all y's even and x's odd putChar(MUSHROOM, x, y); counts[y][x] = 4; } putChar(WAND, wandX, BOTTOM); refresh(); } // StartNewScreen()
void WorldTestUnitAi :: initRingParticles () { static const Vector3 A_CLUMP_CENTER[2] = { Vector3(0.0, 0.0, RING_CLUMP_DISTANCE), Vector3(0.0, 0.0, -RING_CLUMP_DISTANCE), }; for(unsigned int i = 0; i < RING_PARTICLE_COUNT_MAX; i++) { assert(i < RING_PARTICLE_COUNT_MAX); ma_ring_particles[i].m_position = A_CLUMP_CENTER[i % 2] + Vector3::getRandomSphereVector() * RING_CLUMP_RADIUS; double radius1 = random2(RING_PARTICLE_SIZE_MIN, RING_PARTICLE_SIZE_MAX); double radius2 = random2(RING_PARTICLE_SIZE_MIN, RING_PARTICLE_SIZE_MAX); if(radius1 < radius2) ma_ring_particles[i].m_radius = radius1; else ma_ring_particles[i].m_radius = radius2; } assert(invariant()); }
void shotgun::shot() { if(!pressed && time <= 0 && ammo.x) { for(int i = 0; i <= 6; i++) { vec2f r = random2(); level::makeShot(owner->getPos(), owner->getAngle()+3.1415+r.x*0.2, speed, 0.2+r.y*.1, damage); } if(!(--ammo.x)) _reload = reloadTime; } pressed = 2; }
void Splinter::init(const Asteroid &parent, float fi) { _rr = parent.R() /2.; x = parent.X() + parent.R() * cos(fi); y = parent.Y() + parent.R() * sin(fi); _rotateSpeed = parent.rotateSpeed(); float dv = 0.05; vx = parent.VX() + dv * cos(fi); vy = parent.VY() + dv * sin(fi); _color = parent.color(); angle = atan2(vx, vy); nvertices = random2().irandom(12,16); _colorMult = parent.colorMult(); applyParams(); }
Controller::Controller(int width, int height, int palSize, string device, int* baseColors, int numBaseColors, int baseColorsPerPalette, bool layoutLeftToRight, string startDrawerName, int drawerChangeInterval, Camera* camera, FaceDetect* faceDetect) : m_width(width), m_height(height), m_palSize(palSize), m_device(device), m_layoutLeftToRight(layoutLeftToRight), m_startDrawerName(startDrawerName), m_palettes(palSize, baseColors, numBaseColors, baseColorsPerPalette), m_serial(device), m_camera(camera), m_faceDetect(faceDetect), m_currDrawer(NULL), m_drawerChangeTimer(drawerChangeInterval), m_fpsCounter(5000, "Controller") { m_currPalIndex = random2() % m_palettes.size(); m_colIndicesSize = width * height; m_colIndices = new int[m_colIndicesSize]; m_serialWriteBufferSize = width * height * 3 + 1; m_serialWriteBuffer = new unsigned char[m_serialWriteBufferSize]; init(); }
void Controller::changeDrawer(vector<string> names) { string name; assert(names.size() > 0); if (names.size() == 1) name = names[0]; else name = names[random2() % names.size()]; if (m_drawers.find(name) == m_drawers.end()) { cout << "Invalid drawer name: " << name << endl; return; } cout << "Changing to drawer: " << name << endl; m_currDrawer = m_drawers[name]; randomizeSettings(); m_currDrawer->reset(); m_drawerChangeTimer.reset(); }
void gamble_perform (int i) { object room = this_object(); switch (random2(2)) { case 0: { tell_room (room,"\n□□ 坤 签 □□\n"); res[i] = 0; break; } case 1: { tell_room (room,"\n□□ 乾 签 □□\n"); res[i] = 1; break; } } }
////////////////////////////*BARS*/////////////////////////////////////////////// number() { cleardevice(); rectangle(500,130,580,150);outtextxy(503,133,"EXIT"); rectangle(200,100,400,300); for(a=100;a<300;a=a+50) { line(200,a,400,a); } for(a=200;a<400;a=a+50) { line(a,100,a,300); } while(b<=15) { r=random1();r++; h=random2();h++; switch(h) { case 1:x=225;y=125;break; case 2:x=225;y=175;break; case 3:x=225;y=225;break; case 4:x=225;y=275;break; case 5:x=275;y=125;break; case 6:x=275;y=175;break; case 7:x=275;y=225;break; case 8:x=275;y=275;break; case 9:x=325;y=125;break; case 10:x=325;y=175;break; case 11:x=325;y=225;break; case 12:x=325;y=275;break; case 13:x=375;y=125;break; case 14:x=375;y=175;break; case 15:x=375;y=225;break; case 16:x=375;y=275;break; } if(r==16) { ex=x;ey=y; } switch(r) { case 1: outtextxy(x,y,c1); break; case 2: outtextxy(x,y,c2);break; case 3: outtextxy(x,y,c3);break; case 4: outtextxy(x,y,c4);break; case 5: outtextxy(x,y,c5);break; case 6: outtextxy(x,y,c6);break; case 7: outtextxy(x,y,c7);break; case 8: outtextxy(x,y,c8);break; case 9: outtextxy(x,y,c9);break; case 10: outtextxy(x,y,c10);break; case 11: outtextxy(x,y,c11);break; case 12: outtextxy(x,y,c12);break; case 13: outtextxy(x,y,c13);break; case 14: outtextxy(x,y,c14);break; case 15: outtextxy(x,y,c15);break; case 16: outtextxy(x,y,c16);break; } b++; } showmouseptr(); while(flag==0) { getmousepos(&button,&x,&y); if(button==1&&x>500&&x<580&&y>130&&y<150)exit(0); /////////////////main logic////////////// if(button==1&&((x>ex-75&&x<ex-25&&y<ey+25&&y>ey-25)||(x>ex-25&&x<ex+25&&y<ey-25&&y>ey-75)||(x>ex+25&&x<ex+75&&y>ey-25&&y<ey+25)||(x>ex-25&&x<ex+25&&y>ey+25&&y<ey+75))&&(x>200&&x<400&&y<300&&y>100)) { cx=x-(x%50);cy=y-(y%50); size=imagesize(cx+2,cy+2,cx+48,cy+48); buf=malloc(size); hidemouseptr(); getimage(cx+2,cy+2,cx+48,cy+48,buf); putimage(ex-23,ey-23,buf,0); free(buf); setfillstyle(SOLID_FILL,GREEN); bar(cx+2,cy+2,cx+48,cy+48); ex=cx+25;ey=cy+25; showmouseptr(); } ///////////////// } return(0); }
///////////////////////////////////////////////////////// // getforce // ///////////////////////////////////////////////////////// void newWave :: getforce(void) { float d; int i; int j; for (i=0; i<gridX; i++) for ( j=0;j<gridY; j++) { force[i][j] =0.0; } // add (low amplitude) noise to avoid denormalisation. // this noise does propagate thrus the all structure. force[2][2]= 2e-20 * (double)random2() * (1. / 2147483648.) - 1e-20; if (K1 != 0) { for ( i=1; i<gridX; i++) for (int j=1;j<gridY; j++) { d = K1 * (posit[i][j] - posit[i][j-1]); force[i][j] -= d; force[i][j-1] += d; d = K1 * (posit[i][j] - posit[i-1][j]); force[i][j] -= d; force[i-1][j] += d; } } if (K2 != 0) { for ( i=1; i<gridX; i++) for (int j=1;j<gridY; j++) { d = K2 * (posit[i][j] - posit[i-1][j-1]); force[i][j] -= d; force[i-1][j-1] += d; } for ( i=0; i<gridX-1; i++) for (int j=1;j<gridY; j++) { d = K2 * (posit[i][j] - posit[i+1][j-1]); force[i][j] -= d; force[i+1][j-1] += d; } } if (K3 != 0) { for ( i=1; i<gridX-1; i++) for (int j=1;j<gridY-1; j++) { d = K3 * posit[i][j]; force[i][j] -= d; } } }
inherit NPC; int random2 (int i) { return (random(i)+uptime()) % i; } void create() { set_name("¶·¼¦", ({ "dou ji", "ji" }) ); set("race", "Ò°ÊÞ"); set("gender", "ÐÛÐÔ"); set("age", 2); set("long", "Ò»Ö»ÐÛôñôñµÄ¶·¼¦¡£\n"); set("attitude", "friendly"); set("max_kee",400+random2(2)*100); set("max_sen",400+random2(2)); set("str", 40+random2(2)); set("spi", 40+random2(2)); set("cor", 40+random2(2)); set("con", 40+random2(2)); set("cor", 40+random2(2)); set("cps", 40+random2(2)); set_skill("unarmed", random2(2)+10); set_skill("dodge", random2(2)+10); set_skill("parry", random2(2)+10); set("limbs", ({ "Í·²¿", "ÉíÌå", "β°Í" }) ); set("verbs", ({ "bite"}) ); // slow down the douji process. set("allow_heal_up",1);
static int compute_dist(int i,int j, int numvert) { int less, gt; if (i<j) {less = i; gt = j;} else {less = j; gt = i;} return (random2(less*numvert+gt) % RANGE)+1; }
static void randomPlace(char a) { hall[random2(MAX_HALLS)].chair[random2(MAX_ROWS)][random2(MAX_COLUMNS)]=a; }
int main(int argc, char* argv[]) { char* months[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}; int n_prods, i_month, orderline_id, order_id, prod_id, sales, quan_in_stock; char fname[50], line[110]; char filepath[30]; char toinputfile[50]; char output_file[150]; FILE *FP_orderlines; int* pTot_sales; int i_Sys_Type = 0; //0 for Linux, 1 for Windows //Added by GSK //if (argc < 2) //if (argc < 3) //Changed by GSK if (argc < 5) //Changed by WDB adding n_path n_output_file { printf("syntax: ds2_create_inv n_path n_prods n_Sys_Type n_output_file\n"); //Changed by GSK //Changed by WDB printf("n_Sys_Type can be 0 (Linux) or 1 (Windows) \n"); //Added by GSK exit(-1); } strcpy(filepath,argv[1]); //added by WDB n_prods = atoi(argv[2]); i_Sys_Type = atoi(argv[3]); //Added by GSK strcpy(output_file,argv[4]); //Added by WDB char* pch; pch = strstr(output_file,".csv"); if(pch == NULL)//if the string doesn't end in .csv { if(( '/' != output_file[ strlen(output_file) - 1 ] ) && ( '\\' != output_file[ strlen(output_file) - 1 ] ))//if the string doesn't end in a backslash { strcat(output_file,"/inv.csv"); } else { strcat(output_file,"inv.csv"); } } if(( '/' != filepath[ strlen(filepath) - 1 ] ) && ( '\\' != filepath[ strlen(filepath) - 1 ] ))//if the string doesn't end in a backslash { strcat(filepath,"\\"); } pTot_sales = (int *) calloc(n_prods, sizeof(int)); if (pTot_sales == NULL) { printf("Not enough memory\n"); exit(-1); } for (prod_id=0; prod_id<n_prods; prod_id++) pTot_sales[prod_id] = 0; for (i_month=0; i_month<12; i_month++) { strcpy(toinputfile,filepath); strcat(toinputfile,months[i_month]); sprintf(fname, "%s_orderlines.csv", toinputfile); if((FP_orderlines = fopen(fname, "r"))==NULL) { printf("file open error"); break; } while(fgets(line, 100, FP_orderlines)) { sscanf(line, "%d,%d,%d,%d", &orderline_id, &order_id, &prod_id, &sales); pTot_sales[prod_id-1] += sales; } fclose(FP_orderlines); } srand(n_prods); //Seed rand() with n_prods { FILE *fp; fp=fopen(output_file, "w+"); for (prod_id=0; prod_id<n_prods; prod_id++) { quan_in_stock = random2(0,500); if ((prod_id+1) % 10000 == 0) quan_in_stock *= 10; // boost inventory for hot sellers //Changed by GSK //Changed by WDB //commented out for now since it follows the same code path for both linux and windows //this is only valid if you are compiling this on windows with GCC //I will make a valid change for both windows and linux if you want to compile this on linux. fprintf(fp,"%d,%d,%d\n", prod_id+1, quan_in_stock, pTot_sales[prod_id]); // if(i_Sys_Type == 0) //If System type is Linux, Append LF only //Added by GSK // { // fprintf(fp,"%d,%d,%d\n", prod_id+1, quan_in_stock, pTot_sales[prod_id]); // } // else if(i_Sys_Type == 1) //If System type is Windows, Append CR + LF both //Added by GSK // { // fprintf(fp,"%d,%d,%d\r\n", prod_id+1, quan_in_stock, pTot_sales[prod_id]); // } }// end for fclose(fp); }// end write file }
int main (int __argc, char *__argv[]) { peo :: init( __argc, __argv ); if (getNodeRank()==1) std::cout<<"\n\nTest : PSO Global Best\n\n"; rng.reseed (10); RingTopology topologyMig; eoGenContinue < Indi > genContPara (10); eoCombinedContinue <Indi> continuatorPara (genContPara); eoCheckPoint<Indi> checkpoint(continuatorPara); peoEvalFunc<Indi, double, const Indi& > plainEval(f); peoPopEval< Indi > eval(plainEval); eoUniformGenerator < double >uGen (0, 1.); eoInitFixedLength < Indi > random (2, uGen); eoUniformGenerator < double >sGen (-1., 1.); eoVelocityInitFixedLength < Indi > veloRandom (2, sGen); eoFirstIsBestInit < Indi > localInit; eoRealVectorBounds bndsFlight(2,0,1.); eoStandardFlight < Indi > flight(bndsFlight); eoPop < Indi > pop; pop.append (10, random); eoLinearTopology<Indi> topology(2); eoRealVectorBounds bnds(2,-1.,1.); eoStandardVelocity < Indi > velocity (topology,1,0.5,2.,bnds); eoInitializer <Indi> init(eval,veloRandom,localInit,topology,pop); eoPeriodicContinue< Indi > mig_cont( 2 ); peoPSOSelect<Indi> mig_selec(topology); peoGlobalBestVelocity<Indi> mig_replac (2.,velocity); eoContinuator<Indi> cont(mig_cont, pop); eoSelector <Indi, eoPop<Indi> > mig_select (mig_selec,1,pop); eoReplace <Indi, eoPop<Indi> > mig_replace (mig_replac,pop); eoGenContinue < Indi > genContPara2 (10); eoCombinedContinue <Indi> continuatorPara2 (genContPara2); eoCheckPoint<Indi> checkpoint2(continuatorPara2); peoEvalFunc<Indi, double, const Indi& > plainEval2(f); peoPopEval< Indi > eval2(plainEval2); eoUniformGenerator < double >uGen2 (0, 1.); eoInitFixedLength < Indi > random2 (2, uGen2); eoUniformGenerator < double >sGen2 (-1., 1.); eoVelocityInitFixedLength < Indi > veloRandom2 (2, sGen2); eoFirstIsBestInit < Indi > localInit2; eoRealVectorBounds bndsFlight2(2,0,1.); eoStandardFlight < Indi > flight2(bndsFlight2); eoPop < Indi > pop2; pop2.append (10, random2); eoLinearTopology<Indi> topology2(2); eoRealVectorBounds bnds2(2,-1.,1.); eoStandardVelocity < Indi > velocity2 (topology2,1,0.5,2.,bnds2); eoInitializer <Indi> init2(eval2,veloRandom2,localInit2,topology2,pop2); eoPeriodicContinue< Indi > mig_cont2( 2 ); peoPSOSelect<Indi> mig_selec2(topology2); peoGlobalBestVelocity<Indi> mig_replac2 (2.,velocity2); eoContinuator<Indi> cont2(mig_cont2,pop2); eoSelector <Indi, eoPop<Indi> > mig_select2 (mig_selec2,1,pop2); eoReplace <Indi, eoPop<Indi> > mig_replace2 (mig_replac2,pop2); peoAsyncIslandMig< eoPop<Indi>, eoPop<Indi> > mig(cont,mig_select, mig_replace, topologyMig); checkpoint.add( mig ); peoAsyncIslandMig< eoPop<Indi>, eoPop<Indi> > mig2(cont2,mig_select2, mig_replace2, topologyMig); checkpoint2.add( mig2 ); eoSyncEasyPSO <Indi> psa(init,checkpoint,eval, velocity, flight); peoWrapper parallelPSO( psa, pop); eval.setOwner(parallelPSO); mig.setOwner(parallelPSO); eoSyncEasyPSO <Indi> psa2(init2,checkpoint2,eval2, velocity2, flight2); peoWrapper parallelPSO2( psa2, pop2); eval2.setOwner(parallelPSO2); mig2.setOwner(parallelPSO2); peo :: run(); peo :: finalize(); if (getNodeRank()==1) { pop.sort(); pop2.sort(); std::cout << "Final population :\n" << pop << std::endl; std::cout << "Final population :\n" << pop2 << std::endl; } }