int main() { srand(time(NULL)); int M = myrand(100); int N = myrand(); printf("(M,N) = (%d,%d)\n", M,N); StructArr arrM = oct2bin(M); StructArr arrN = oct2bin(N); insertNum(arrN, arrM); insertNumBit(N, M); return 0; }
int random(int min, int max) { static int flag=0; if(flag==0){ mysrand(uptime()); myrand(); flag=1; }else{ myrand(); } int rand = min+rand_seed%(max-min); return rand; }
void initParticles(cData *p, int dx, int dy) { int i, j; for (i = 0; i < dy; i++) { for (j = 0; j < dx; j++) { p[i*dx+j].x = (j+0.5f+(myrand() - 0.5f))/dx; p[i*dx+j].y = (i+0.5f+(myrand() - 0.5f))/dy; } } }
void initParticles(cData *p, int dx, int dy) { for (int i = 0; i < dy; i++) { for (int j = 0; j < dx; j++) { p[i*dx+j].x = (j+0.5f+(myrand() - 0.5f))/dx; p[i*dx+j].y = (i+0.5f+(myrand() - 0.5f))/dy; } } unsigned char* data; unsigned int width, height; bool loaded = sdkLoadPPM4ub("data/usi.ppm", &data, &width, &height); if (!loaded) return; const char* no_label = getenv("NO_LABEL"); if (!no_label) { const int nchannels = 4; for (int j = 0; j < height; j++) for (int i = 0; i < width; i++) { unsigned char* pixel = &data[nchannels * (width * j + i)]; if ((pixel[0] == 0) && (pixel[1] == 0) && (pixel[2] == 0)) { p[j*dx+i].x = 0; p[j*dx+i].y = 0; } } } #ifdef BROADCAST // Randomly reorder particles to eliminate initial jittering // in UDP client. for (int i = 0; i < dy; i++) { for (int j = 0; j < dx; j++) { int i_rand = myrand() * (dx - 1); int j_rand = myrand() * (dy - 1); cData* p0 = &p[j*dx+i]; cData* p1 = &p[j_rand*dx+i_rand]; cData p2 = *p0; *p0 = *p1; *p1 = p2; } } #endif }
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n, u32 active_object_count, u32 active_object_count_wider) { INodeDefManager *ndef = env->getGameDef()->ndef(); ServerMap *map = &env->getServerMap(); MapNode n_below = map->getNodeNoEx(p - v3s16(0, 1, 0)); if (n_below.getContent() != c_dirt && n_below.getContent() != c_dirt_with_grass) return; bool is_jungle_tree = n.getContent() == c_junglesapling; actionstream <<"A " << (is_jungle_tree ? "jungle " : "") << "sapling grows into a tree at " << PP(p) << std::endl; std::map<v3s16, MapBlock*> modified_blocks; v3s16 tree_p = p; ManualMapVoxelManipulator vmanip(map); v3s16 tree_blockp = getNodeBlockPos(tree_p); vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1)); if (is_jungle_tree) { treegen::make_jungletree(vmanip, tree_p, ndef, myrand()); } else { bool is_apple_tree = myrand() % 4 == 0; treegen::make_tree(vmanip, tree_p, is_apple_tree, ndef, myrand()); } vmanip.blitBackAll(&modified_blocks); // update lighting std::map<v3s16, MapBlock*> lighting_modified_blocks; lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end()); map->updateLighting(lighting_modified_blocks, modified_blocks); // Send a MEET_OTHER event MapEditEvent event; event.type = MEET_OTHER; // event.modified_blocks.insert(modified_blocks.begin(), modified_blocks.end()); for(std::map<v3s16, MapBlock*>::iterator i = modified_blocks.begin(); i != modified_blocks.end(); ++i) { event.modified_blocks.insert(i->first); } map->dispatchEvent(&event); }
void heartbeat(void) { time_t now = time(NULL); struct tm *ptm = localtime(&now); static int x = 0, y = 0, dx = 1, dy = 1; static NEWWIN *hb_win = NULL; x += dx; y += dy; if (x >= (max_x - 8)) { dx = -(myrand(1) + 1); x = max_x - (8 + 1); } else if (x < 0) { dx = (myrand(2) + 1); x = 0; } if (y >= max_y) { dy = -(myrand(2) + 1); y = max_y - 1; } else if (y < 0) { dy = (myrand(2) + 1); y = 0; } if (dx == 0 && dy == 0) { dy = 1; dy = -1; } if (!hb_win) { hb_win = create_popup(1, 8); } move_panel(hb_win -> pwin, y, x); ui_inverse_on(hb_win); mvwprintw(hb_win -> win, 0, 0, "%02d:%02d:%02d", ptm -> tm_hour, ptm -> tm_min, ptm -> tm_sec); ui_inverse_off(hb_win); mydoupdate(); }
//更新位置等相关属性 void updateobj(AniObj* obj) { // 当前位置 + 速度 obj->x += obj->dx; obj->y += obj->dy; if (obj->x < 0) obj->dx = myrand(randspeed) + base_speed; //碰左 if (obj->y < 0) obj->dy = myrand(randspeed) + base_speed; //碰上 if (obj->x >= ege::getwidth() - obj->r * 2) obj->dx = -(myrand(randspeed) + base_speed); //碰右 if (obj->y >= ege::getheight() - obj->r * 2) obj->dy = -(myrand(randspeed) + base_speed); //碰下 // 改变alpha值 obj->alpha += obj->da; if (obj->alpha <= 0) obj->da = 1; if (obj->alpha >= 0xFF) obj->da = -1; }
void *put_many(void *arg) { struct arg_struct *arg_s = (struct arg_struct *) arg; printf("started thread %d\n", arg_s->thread_number); for (int i = 0; i < arg_s->count; i++) { int x = arg_s->start_val + myrand(1000); time_t t0 = time(NULL); put(arg_s->cyclic_buffer, x); size_t s = get_cyclic_buffer_size(arg_s->cyclic_buffer); time_t t1 = time(NULL); printf("PUT %4d: x=%7d s=%4zd t=%3d\n", arg_s->thread_number, x, s, (int) (t1-t0)); sleep(myrand(arg_s->delay_limit)); } printf("finishing thread %d\n", arg_s->thread_number); return NULL; }
static HOST *dict_mysql_find_host(PLMYSQL *PLDB, unsigned stat, unsigned type) { time_t t; int count = 0; int idx; int i; t = time((time_t *) 0); for (i = 0; i < PLDB->len_hosts; i++) { if (dict_mysql_check_stat(PLDB->db_hosts[i], stat, type, t)) count++; } if (count) { /* * Calling myrand() can deplete the random pool. * Don't rely on the optimizer to weed out the call * when count == 1. */ idx = (count > 1) ? 1 + (count - 1) * (double) myrand() / RAND_MAX : 1; for (i = 0; i < PLDB->len_hosts; i++) { if (dict_mysql_check_stat(PLDB->db_hosts[i], stat, type, t) && --idx == 0) return PLDB->db_hosts[i]; } } return 0; }
//helper function to return a hash value for Flajolet & Martin bitmask size_t hash_value() { size_t ret = 0; while (myrand() < 0.5) { ret++; } return ret; }
static struct projectile *shrapnel(struct projectile *parent) { struct projectile *p = get_projectile (); if (! p) return 0; p->x = parent->x; p->y = parent->y; p->dx = (myrand() % 5000) - 2500 + parent->dx; p->dy = (myrand() % 5000) - 2500 + parent->dy; p->decay = (myrand() % 50) - 60; p->size = (parent->size * 2) / 3; p->fuse = 0; p->primary = false; p->colour = parent->colour; return p; }
void init_wator (int maxi, int maxj, int hbrut, int fbrut, int fasten) { register int i, j, res; register struct animal *v = &(wator[0][0]); HBRUT = hbrut; FBRUT = fbrut; FASTEN = fasten; assert (hbrut - FASTEN> 0); for (i=0; i<MAXI; ++i) { for (j=0; j<MAXJ; ++j) { res = myrand (30); v->kind = EMPTY; switch (res) { case 0: new_animal ( v ,SHARK);//i, j, SHARK); break; case 1 ... 11: new_animal ( v ,FISH);//i, j, FISH); break; } ++v; } } }
/* try swapping chains at different temperatures */ void chaincolln_tryswaps(chaincolln cc) { int nchains; int sw1, sw2, swcount; chain c1, c2; double t1, t2, aswap, rn; nchains = cc->nchains; if (nchains > 1) { for (swcount = 0; swcount < 1; swcount++) { sw1 = randomitem(nchains); sw2 = sw1; while (sw2 == sw1) { sw2 = randomitem(nchains); } c1 = cc->chains[sw1]; c2 = cc->chains[sw2]; t1 = chain_gettemp(c1); t2 = chain_gettemp(c2); aswap = exp( (t1 - t2)* (chain_getprob(c2) - chain_getprob(c1)) ); if (aswap > 1) { aswap=1; } rn = myrand(); if (rn < aswap) { chaincolln_swapchains(cc, sw1, sw2); fprintf(stderr, "****CS: %f\n", aswap); } } } }
//shuffle vector interval indexed a..b void shuffle_interval(int X[], int a, int b) { int j,k; for (k = b; k > a; k--) { j = myrand(a,k); //printf("rand %d\n", j); swap(X[j],X[k]); } }
/************************************************************************* Test one aspect of randomness, using n numbers. Reports results to LOG_TEST; with good randomness, behaviourchange and behavioursame should be about the same size. Tests current random state; saves and restores state, so can call without interrupting current sequence. *************************************************************************/ void test_random1(int n) { RANDOM_STATE saved_state; int i, old_value = 0, new_value; bool didchange, olddidchange = FALSE; int behaviourchange = 0, behavioursame = 0; saved_state = get_myrand_state(); /* mysrand(time(NULL)); */ /* use current state */ for (i = 0; i < n+2; i++) { new_value = myrand(2); if (i > 0) { /* have old */ didchange = (new_value != old_value); if (i > 1) { /* have olddidchange */ if (didchange != olddidchange) { behaviourchange++; } else { behavioursame++; } } olddidchange = didchange; } old_value = new_value; } freelog(LOG_TEST, "test_random1(%d) same: %d, change: %d", n, behavioursame, behaviourchange); /* restore state: */ set_myrand_state(saved_state); }
/* thread the remove function */ static void *threadremove(void *targ){ TCMDB *mdb = ((TARGCOMBO *)targ)->mdb; TCNDB *ndb = ((TARGCOMBO *)targ)->ndb; int rnum = ((TARGCOMBO *)targ)->rnum; bool rnd = ((TARGCOMBO *)targ)->rnd; int id = ((TARGCOMBO *)targ)->id; double stime = tctime(); if(id == 0) iprintf("removing:\n"); int base = id * rnum; for(int i = 1; i <= rnum; i++){ char buf[RECBUFSIZ]; int len = sprintf(buf, "%08d", base + (rnd ? myrand(i) : i)); if(ndb){ tcndbout(ndb, buf, len); } else { tcmdbout(mdb, buf, len); } if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } if(id == 0) iprintf("time: %.3f\n", tctime() - stime); return NULL; }
void corpus_initialize_ss(lda_suffstats* ss, lda_model* model, corpus* c) { int num_topics = model->num_topics; int i, k, d, n; document* doc; for (k = 0; k < num_topics; k++) { for (i = 0; i < NUM_INIT; i++) { d = floor(myrand() * c->num_docs); printf("initialized with document %d\n", d); doc = &(c->docs[d]); for (n = 0; n < doc->length; n++) { ss->class_word[k][doc->words[n]] += doc->counts[n]; } } for (n = 0; n < model->num_terms; n++) { ss->class_word[k][n] += 1.0; ss->class_total[k] = ss->class_total[k] + ss->class_word[k][n]; } } }
static HOST *dict_mysql_find_host(PLMYSQL *PLDB, unsigned stat, unsigned type) { time_t t; int count = 0; int idx; int i; t = time((time_t *) 0); for (i = 0; i < PLDB->len_hosts; i++) { if (dict_mysql_check_stat(PLDB->db_hosts[i], stat, type, t)) count++; } if (count) { idx = (count > 1) ? 1 + count * (double) myrand() / (1.0 + RAND_MAX) : 1; for (i = 0; i < PLDB->len_hosts; i++) { if (dict_mysql_check_stat(PLDB->db_hosts[i], stat, type, t) && --idx == 0) return PLDB->db_hosts[i]; } } return 0; }
static const char *dict_random_lookup(DICT *dict, const char *unused_query) { DICT_RANDOM *dict_random = (DICT_RANDOM *) dict; DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, dict_random->replies->argv[myrand() % dict_random->replies->argc]); }
void ShuffleDeck()//function to shuffle deck { for(i=0;i<4;i++) { for (j = 1; j <=13; j++) { store = deck.face[j+(i*13)];// shuffle = myrand(min,max);//store rand num in shuffle deck.face[j+(i*13)] = deck.face[shuffle];//stores random number inside array deck.face[shuffle] = store; s = deck.suit[j+(i*13)]; deck.suit[j+(i*13)] = deck.suit[shuffle]; deck.suit[shuffle] = s;//swap function for s and deck.suit } } printf("Deck of cards after shuffling!\n\n"); for(i=0;i<4;i++) { for( j=1;j<=13;j++) { printf("%c%d ",deck.suit[j+(i*13)],deck.face[j+(i*13)]);//for loop for printing shuffled cards (face and suit) } } printf("\n\n"); fflush(stdin); }
/* set the particles in sys. * first and last are the indices of the particles which will be set. * particles are initialized with zero acceleration and velocity, and * randomly distributed within the system. * mass is the particle's mass. * returns 0 if particles set correctly*/ int setParticles(System *sys, int first, int last, float mass){ Particle *part,*part0,*part1; int alpha; if(first<0 || first>last ||last>=sys->nbParts) return 1; part0=sys->parts+first; part1=sys->parts+last+1; for(part=part0;part<part1;part++){ part->nextPart=NULL; part->mass=mass; part->radius=0.02; part->cor=0.8; //part->k=GAS_CONST*TEMP/0.01801528; //for water part->k=10; //P=k*ro; P0=1Bar=10^5Pa = k*ro0 part->k=3; //P=k*ro; P0=1Bar=10^5Pa = k*ro0 part->ro0=1000.0; //for water part->mu=1e-3; //for water part->mu=10;//test part->sigma=72.8e-3; //for water for(alpha=0;alpha<3;alpha++){ part->fg[alpha]=part->fp[alpha]=part->fs[alpha]=part->fv[alpha]=0; part->vel[alpha]=0; part->pos[alpha]=myrand()*(sys->boundaries[alpha][1]-sys->boundaries[alpha][0]-2.01*part->radius)+sys->boundaries[alpha][0]+1.005*part->radius; } // part->pos[0]=myrand()*(0.5-2.01*part->radius)+0.5+1.005*part->radius; // part->pos[2]=myrand()*(0.5-2.01*part->radius)+0.5+1.005*part->radius; part->fg[0]=0; part->fg[1]=0; part->fg[2]=-9.80665; //g in m/s^2 } setParticlesGrid(sys->grid, sys->parts, sys->nbParts); return 0; }
/************************************************************************* Initialize the generator; see comment at top of file. *************************************************************************/ void mysrand(RANDOM_TYPE seed) { int i; rand_state.v[0]=(seed & MAX_UINT32); for(i=1; i<56; i++) { rand_state.v[i] = (3 * rand_state.v[i-1] + 257) & MAX_UINT32; } rand_state.j = (55-55); rand_state.k = (55-24); rand_state.x = (55-0); rand_state.is_init = TRUE; /* Heat it up a bit: * Using modulus in myrand() this was important to pass * test_random1(). Now using divisor in myrand() that particular * test no longer indicates problems, but this seems a good idea * anyway -- eg, other tests could well reveal other initial * problems even using divisor. */ for (i=0; i<10000; i++) { (void) myrand(MAX_UINT32); } }
int main(int argc, char ** argv) { srand48(time(NULL)); long ni=10,nj=10,max=100; int offset = 0; if (argc > ++offset) ni = atoi( argv[offset] ); if (argc > ++offset) nj = atoi( argv[offset] ); if (argc > ++offset) max = atoi( argv[offset] ); long tmp; printf("%ld %ld M\n", ni, nj); for (long i = 0; i < ni; ++i) for (long j = 0; j < nj; ++j){ printf("%ld %ld %ld\n", i+1, j+1, myrand(tmp, max)); } printf("0 0 0\n"); return 0; }
int choose_fish (int i, int j) { int k = 0; int dir = 0; int dirs[4]; //primer ciclo struct animal * t = &wator[ni (i, dir)][nj (j, dir)]; if (t->kind - FISH == 0) dirs[k++] = dir; //segundo ciclo ++dir; t = &wator[ni (i, dir)][nj (j, dir)]; if (t->kind - FISH == 0) dirs[k++] = dir; //tercer ciclo ++dir; t = &wator[ni (i, dir)][nj (j, dir)]; if (t->kind - FISH == 0) dirs[k++] = dir; //cuarto ciclo ++dir; t = &wator[ni (i, dir)][nj (j, dir)]; if (t->kind - FISH == 0) dirs[k++] = dir; if (k == 0) return REST; return dirs[myrand (k)]; }
std::string TestBase::getTestTempFile() { char buf[32]; snprintf(buf, sizeof(buf), "%08X", myrand()); return getTestTempDirectory() + DIR_DELIM + buf + ".tmp"; }
void naryRandom::generateNaryCtr( vector<int>& indexs, long nogoods, Cost costMin, Cost costMax) { int i; int arity = indexs.size(); EnumeratedVariable** scopeVars = new EnumeratedVariable * [arity]; int* scopeIndexs = new int [arity]; Char* tuple = new Char [arity+1]; Cost Top = wcsp.getUb(); if(costMax < Top) Top = costMax; for(i = 0; i<arity; i++) { scopeIndexs[i] = indexs[i]; scopeVars[i] = (EnumeratedVariable*) wcsp.getVar(indexs[i]); tuple[i] = 0 + CHAR_FIRST; } tuple[arity] = '\0'; Constraint* nctr = wcsp.getCtr( wcsp.postNaryConstraintBegin(scopeIndexs, arity, Top) ); String s(tuple); while(nogoods>0) { for(i = 0; i<arity; i++) s[i] = myrand() % scopeVars[i]->getDomainInitSize() + CHAR_FIRST; Cost c = ToulBar2::costMultiplier * randomCost(MIN_COST, costMax); nctr->setTuple(s, c, scopeVars); nogoods--; } nctr->propagate(); delete [] scopeIndexs; delete [] scopeVars; delete [] tuple; }
int main(int argc, char *argv[]) { int *p,size,length,h,w; h = makeeven(40); w = makeeven(50); length = 80; // 1 - 100 length = length == 0?70:length; ++h; ++w; size = h * w; /* allocate our array to hold lab. data*/ a = (int *)calloc(size, sizeof(int)); if(a != NULL) printf("\n\t%d bytes of memory allocated\n",size * sizeof(int)); else { printf("Memory allocation failed\n"); free(a); return 1; } printf("\n\tprinting labyrinth, dimentions %d x %d...\n\n\t",w-1,h-1); //собственно построение createborder(size,w,h); createdoors(size,w); /*set random seed*/ randomize(); int x = 0,z = 0; cycle+= size + size/2; while(getrandom(w,h,&x) > 0) { if(a[x] == 1) continue; z = getdirection(w); //printf("%5d",x); /*here we're building the walls, creating derived points*/ do { if(a[x] == 0) a[x] = 1; x += z; if(myrand(100) > length) break; } while(a[x] == 0 ); } display(size,w); cleanup(); return 0; }
void naryRandom::generateTernCtr( int i, int j, int k, long nogoods, Cost costMin, Cost costMax ) { int a,b,c,dice; EnumeratedVariable* x = (EnumeratedVariable*) wcsp.getVar(i); EnumeratedVariable* y = (EnumeratedVariable*) wcsp.getVar(j); EnumeratedVariable* z = (EnumeratedVariable*) wcsp.getVar(k); int mx = x->getDomainInitSize(); int my = y->getDomainInitSize(); int mz = z->getDomainInitSize(); long total_nogoods = mx*my*mz; vector<Cost> costs; for (a = 0; a < mx; a++) for (b = 0; b < my; b++) for (c = 0; c < mz; c++) costs.push_back(MIN_COST); while(nogoods>0) { dice = myrand() % total_nogoods; for(a=0;a<mx;a++) for(b=0;b<my;b++) for(c=0;c<mz;c++) { if(costs[my*mz*a + b*mz + c] == MIN_COST) { if(dice == 0) { costs[my*mz*a + b*mz + c] = ToulBar2::costMultiplier * randomCost(costMin, costMax); nogoods--; total_nogoods--; a=mx;b=my;c=mz; } dice--; } } } wcsp.postTernaryConstraint(i,j,k,costs); }
int Solver::getVarMinDomainDivMaxDegreeRandomized(Cluster* cluster) { if (unassignedVars->empty()) return -1; int varIndex = -1; Cost worstUnaryCost = MIN_COST; double best = MAX_VAL - MIN_VAL; int ties[cluster->getNbVars()]; int nbties = 0; for (TVars::iterator iter = cluster->beginVars(); iter != cluster->endVars(); ++iter) { if (wcsp->unassigned(*iter)) { int deg = wcsp->getDegree(*iter) + 1; // - ((WCSP *)wcsp)->getVar(*iter)->nbSeparators(); double heuristic = (double)wcsp->getDomainSize(*iter) / (double)max(deg, 1); if (varIndex < 0 || heuristic < best - epsilon * best || (heuristic < best + epsilon * best && wcsp->getMaxUnaryCost(*iter) > worstUnaryCost)) { best = heuristic; varIndex = *iter; nbties = 1; ties[0] = varIndex; worstUnaryCost = wcsp->getMaxUnaryCost(*iter); } else if (heuristic < best + epsilon * best && wcsp->getMaxUnaryCost(*iter) == worstUnaryCost) { ties[nbties] = *iter; nbties++; } } } if (nbties > 1) return ties[myrand() % nbties]; else return varIndex; }
int choose_fish (int i, int j) { register int k = 0; register int dir = 0; int dirs[4]; //primer ciclo, dir = 0 => NORTH register struct animal * t = &wator[((i+MAXI-1) & 0x1F )][j]; if (t->kind - FISH == 0) dirs[k++] = dir; //segundo ciclo, dir = 1 => SOUTH ++dir; t = &wator[((i+1) & 0x1F )][j]; if (t->kind - FISH == 0) dirs[k++] = dir; //tercer ciclo, dir = 2 => EAST ++dir; t = &wator[i][((j+MAXJ-1) & 0x1F )]; if (t->kind - FISH == 0) dirs[k++] = dir; //cuarto ciclo, dir = 3 => WEST ++dir; t = &wator[i][((j+1) & 0x1F )]; if (t->kind - FISH == 0) dirs[k++] = dir; // if (k == 0) // return REST; // return dirs[myrand (k)]; return ((k==0) ? REST : dirs[myrand(k)] ); }