void pingpong_mutate_shift(population *pop, entity *mother, entity *son) { int i, j, k; /* Team members. */ int tmp; /* For swapping i and j. */ /* Copy chromosomes of parent to offspring. */ memcpy( son->chromosome[0], mother->chromosome[0], pop->len_chromosomes*sizeof(int) ); i = random_int(9); do { j = random_int(9); } while(i==j); if (i>j) { tmp = ((int *)son->chromosome[0])[j]; for (k=j; k<i; k++) { ((int *)son->chromosome[0])[k] = ((int *)son->chromosome[0])[k+1]; } ((int *)son->chromosome[0])[i] = tmp; } else { tmp = ((int *)son->chromosome[0])[j]; for (k=j; k>i; k--) { ((int *)son->chromosome[0])[k] = ((int *)son->chromosome[0])[k-1]; } ((int *)son->chromosome[0])[i] = tmp; } return; }
void init(void) { fault_handling_setup(); reset_callback_register(); nap_version_check(); nap_auth_check(); nap_callbacks_setup(); rng_setup(); srand(random_int()); stm_unique_id_callback_register(); }
Node* generate_terminal_node(std::mt19937& random, bool is_ghost) { if (is_ghost) { switch (random_int(random, 0, 2)) { case 0: return createNodeFromInt(random, Constant); break; case 1: return createNodeFromInt(random, GhostToGhost); break; case 2: return createNodeFromInt(random, GhostToPacman); break; } } else { switch (random_int(random, 0, 3)) { case 0: return createNodeFromInt(random, Constant); break; case 1: return createNodeFromInt(random, PacmanToDot); break; case 2: return createNodeFromInt(random, PacmanToGhost); break; case 3: return createNodeFromInt(random, PacmanDotsEaten); break; } } return nullptr; }
void wildfire_mutate_flip(population *pop, entity *mother, entity *son) { int i; /* Map square. */ /* Copy chromosome. */ for(i=0; i<WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION; i++) ((int *)son->chromosome[0])[i] = ((int *)mother->chromosome[0])[i]; /* Mutation. */ i = random_int(WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION); ((int *)son->chromosome[0])[i] = !((int *)son->chromosome[0])[i]; return; }
pointer list_to_int(pointer a) { int res=0; char *dispatch; if (is_symbol(car(a))) { dispatch=sym_name(car(a)); if (!strcmp(dispatch,"random")) return random_int(cadr(a),caddr(a)); else return Error_1("list>int: unknown dispatch type:",car(a)); } else return Error_1("list>int: no symbol:",car(a)); return mk_integer(res); }
void anneal(instance *t, solution *s) { double temperature = INITIAL_TEMPERATURE; initialize_solution(t->n, s); double current_value = solution_cost(s, t); for (int i = 1; i <= COOLING_STEPS; i++) { temperature *= COOLING_FRACTION; double start_value = current_value; for (int j = 1; j <= STEPS_PER_TEMP; j++) { int i1 = random_int(1, t->n); int i2 = random_int(2, t->n); double flip = random_float(0, 1); double delta = transition(s, t, i1, i2); double exponent = (-delta / current_value) / (K * temperature); double merit = pow(E, exponent); if (delta < 0) current_value = current_value + delta; else if (merit > flip) current_value = current_value + delta; else transition(s, t, i1, i2); } if ((current_value - start_value) < 0.0) temperature = temperature / COOLING_FRACTION; } }
RTPSession *rtp_new ( int payload_type, Messenger *messenger, int friend_num ) { RTPSession *retu = calloc(1, sizeof(RTPSession)); if ( !retu ) { LOGGER_WARNING("Alloc failed! Program might misbehave!"); return NULL; } if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, retu)) { LOGGER_ERROR("Error setting custom register handler for rtp session"); free(retu); return NULL; } LOGGER_DEBUG("Registered packet handler: pt: %d; fid: %d", payload_type, friend_num); retu->version = RTP_VERSION; /* It's always 2 */ retu->padding = 0; /* If some additional data is needed about the packet */ retu->extension = 0; /* If extension to header is needed */ retu->cc = 1; /* Amount of contributors */ retu->csrc = NULL; /* Container */ retu->ssrc = random_int(); retu->marker = 0; retu->payload_type = payload_type % 128; retu->dest = friend_num; retu->rsequnum = retu->sequnum = 0; retu->ext_header = NULL; /* When needed allocate */ if ( !(retu->csrc = calloc(1, sizeof (uint32_t))) ) { LOGGER_WARNING("Alloc failed! Program might misbehave!"); free(retu); return NULL; } retu->csrc[0] = retu->ssrc; /* Set my ssrc to the list receive */ /* Also set payload type as prefix */ retu->prefix = payload_type; /* * */ return retu; }
int random_int_() /*@ requires [?f]world(?pub, ?key_clsfy) &*& proof_obligations(pub) &*& principal(?principal, ?count) &*& true == bad(principal); @*/ /*@ ensures [f]world(pub, key_clsfy) &*& proof_obligations(pub) &*& principal(principal, count + 1); @*/ { //@ open proof_obligations(pub); //@ assert is_public_nonce(?proof, _); //@ proof(nonce_item(principal, count + 1, 0)); return random_int(); //@ close proof_obligations(pub); }
int test_random() { int i, j, result = 0; test_msg_start("Test Random Number Generator - Integers 0 to 100"); random_seed(); j = 0; while(j < 100000) { i = random_int(0,100); if(i < 0 || i > 100) result++; j++; } test_msg_end(result); return result; }
void MixtureDataPolicy::add_data(const Ptr<DataType> &d) { dat().push_back(d); int max_levels = pkey_->max_levels(); if (max_levels > 0) { uint h = random_int(0, max_levels - 1); NEW(CategoricalData, pcat)(h, pkey_); latent_data().push_back(pcat); } else { NEW(CategoricalData, pcat)(uint(0), pkey_); latent_data().push_back(pcat); } if (!known_data_source_.empty()) { known_data_source_.push_back(-1); } }
const GIndividual* GPopulation::choose_individual() const { if (gd.selection_method == GenDef::SELECTION_TOURNAMENT) { const GIndividual* a1 = individuals[ random_int( individuals.size() ) ]; const GIndividual* a2 = individuals[ random_int( individuals.size() ) ]; if (gd.use_parsimony && a1->raw_fitness >= gd.parsimony_threshhold && a2->raw_fitness >= gd.parsimony_threshhold) { if (fabs( a1->raw_fitness - a2->raw_fitness ) <= 1.0) { if ( a1->alg->num_children() < a2->alg->num_children() ) return a1; else return a2; } } if (a1->raw_fitness > a2->raw_fitness) return a1; else return a2; } else // fitness proportionate { double f = static_cast<double>(random_float( 1.0 )); for( unsigned i = 0; i < individuals.size(); i++ ) { f -= individuals[i]->normalized_fitness; if (f <= 0.0f) return individuals[i]; } } return individuals[0]; }
void addenemy(unsigned short type) { int i; enemy *typ; if (type >= sizeof(enemy_list)) type = sizeof(enemy_list)-1; typ = (enemy*)&enemy_list[type]; for (i = 0; i < MAX_ENEMIES; i++) { enemy_ref *curr = &enemies[i]; if (curr->health >= 0) continue; curr->type = typ; curr->health = typ->start_health; curr->counter = 0; if (typ->start_x < 0) curr->x = (random_int() % (127-(typ->size<<1))) + typ->size + 1; else curr->x = typ->start_x; if (typ->start_y < 0) curr->y = (random_int() % (95-(typ->size<<1))) + typ->size + 1; else curr->y = typ->start_y; break; } }
void choose_reduced_pilots() { int i, reduced, count, num_pilots = 2 * m; if (k > 0) { reduced = (num_pilots * k / 100.0); count = 0; while (count < reduced) { i = random_int(0, num_pilots - 1); if (!pilots[i]->reduce) { ++count; pilots[i]->reduce = 1; } } } }
static int * random_array (int minval, int maxval, size_t len) { int * arr; int * ii; arr = malloc (len * sizeof(int)); if (NULL == arr) { err (EXIT_FAILURE, __FILE__": %s: malloc", __func__); } for (ii = arr; len > 0; ++ii, --len) { *ii = random_int (minval, maxval); } return arr; }
void CNetTokenManager::GenerateSeed() { static const NETADDR NullAddr = { 0 }; m_PrevSeed = m_Seed; for(int i = 0; i < 2; i++) { m_Seed <<= 32; m_Seed ^= random_int(); } m_PrevGlobalToken = m_GlobalToken; m_GlobalToken = GenerateToken(&NullAddr); m_NextSeedTime = time_get() + time_freq() * m_SeedTime; }
JSBool js_event_start_wait_random_func(JSContext *cx,JSObject *j_obj,uintN argc,jsval *argv,jsval *rval) { int min,max,tick; min=JSVAL_TO_INT(argv[0]); max=JSVAL_TO_INT(argv[1]); tick=random_int(abs(max-min))+min; if (!timers_add(&js.attach,tick,JSVAL_TO_INT(argv[2]),NULL,timer_mode_single)) { *rval=JSVAL_FALSE; } else { *rval=JSVAL_TRUE; } return(JS_TRUE); }
Node* generate_tree_up_to(std::mt19937& random, int max_levels, bool is_ghost) { if (max_levels <= 0) return nullptr; if (max_levels == 1) { return generate_terminal_node(random, is_ghost); } else { Node* parent = generate_parent_node(random); for (int child_index = 0; child_index < parent->GetMinChildren(); ++child_index) { parent->children.emplace_back(generate_tree(random, random_int(random, 1, max_levels - 1), is_ghost)); } return parent; } }
void screen_fx_random(){ int x, y; random_seed(); for(x=0; x < WIDTH; x++){ for(y = 0; y < HEIGHT; y++){ s[x][y].bg_bl = (float)random_int(1,10000)/10000; s[x][y].bg_rd = (float)random_int(1,10000)/10000; s[x][y].bg_gr = (float)random_int(1,10000)/10000; s[x][y].fg_bl = (float)random_int(1,10000)/10000; s[x][y].fg_rd = (float)random_int(1,10000)/10000; s[x][y].fg_gr = (float)random_int(1,10000)/10000; s[x][y].ch = random_int(0, 128); } } }
void robot_manager::add_user(room_user uinfo) { guard g(m_mutex); bool is_ok = false; do { if(uinfo.coin < config_manager::instance()->get_ac().less_coin) { break; } else if(config_manager::instance()->get_ac().max_coin > 0 && uinfo.coin > config_manager::instance()->get_ac().max_coin) { break; } is_ok = true; } while (false); if(!is_ok) { if(!cfg_inst->is_ai_auto_supplement()) { db_thread::instance()->on_logout(uinfo.uid); return; } int coin = random_int(cfg_inst->ai_supplement_from(), cfg_inst->ai_supplement_to()); int dc = (int)(coin - uinfo.coin); uinfo.coin = coin; db_thread::instance()->on_add_coin(AI_SUPPLEMENT, uinfo.uid, dc); } map<unsigned int, room_user>::iterator i; i = m_robot.find(uinfo.uid); if(i == m_robot.end()) { m_robot[uinfo.uid] = uinfo; m_idle.push(&m_robot[uinfo.uid]); i = m_robot.find(uinfo.uid); } else { i->second.coin = uinfo.coin; i->second.vip = uinfo.vip; } }
GAULFUNC void random_int_permutation(const int size, int *iarray, int *oarray) { int i,j=0; /* Loop variables over arrays. */ int pos; /* Randomly selected index. */ if (!iarray || !oarray) die("NULL pointer to int array passed."); for (i=size-1; i>0; i--) { pos = random_int(i); oarray[j++] = iarray[pos]; iarray[pos] = iarray[i]; } oarray[j] = iarray[0]; return; }
Project(Display *display, Player *player) : NewWorldScreen(display, player) , bitmaps("data") , motion() , tile_index() { level = new Level(); current_map = new Map(); level->maps.push_back(current_map); tile_index.load_from_atlas(bitmaps["spritesheet.png"], 21, 21, 2, 2, 1, 1); current_map->tile_layers.push_back(TileLayer(&tile_index, 60, 60)); // fill our map with random tiles for (unsigned x=0; x<current_map->tile_layers.back().width; x++) for (unsigned y=0; y<current_map->tile_layers.back().height; y++) current_map->tile_layers.back().set_tile(x, y, random_int(0, 200)); }
void CEffects::PlayerDeath(vec2 Pos, int ClientID) { vec3 BloodColor(1.0f,1.0f,1.0f); if(ClientID >= 0) { if(m_pClient->m_GameInfo.m_GameFlags&GAMEFLAG_TEAMS) { int ColorVal = m_pClient->m_pSkins->GetTeamColor(m_pClient->m_aClients[ClientID].m_aUseCustomColors[CSkins::SKINPART_BODY], m_pClient->m_aClients[ClientID].m_aSkinPartColors[CSkins::SKINPART_BODY], m_pClient->m_aClients[ClientID].m_Team, CSkins::SKINPART_BODY); BloodColor = m_pClient->m_pSkins->GetColorV3(ColorVal); } else { if(m_pClient->m_aClients[ClientID].m_aUseCustomColors[CSkins::SKINPART_BODY]) BloodColor = m_pClient->m_pSkins->GetColorV3(m_pClient->m_aClients[ClientID].m_aSkinPartColors[CSkins::SKINPART_BODY]); else { const CSkins::CSkinPart *s = m_pClient->m_pSkins->GetSkinPart(CSkins::SKINPART_BODY, m_pClient->m_aClients[ClientID].m_SkinPartIDs[CSkins::SKINPART_BODY]); if(s) BloodColor = s->m_BloodColor; } } } for(int i = 0; i < 64; i++) { CParticle p; p.SetDefault(); p.m_Spr = SPRITE_PART_SPLAT01 + (random_int()%3); p.m_Pos = Pos; p.m_Vel = RandomDir() * ((frandom()+0.1f)*900.0f); p.m_LifeSpan = 0.3f + frandom()*0.3f; p.m_StartSize = 24.0f + frandom()*16; p.m_EndSize = 0; p.m_Rot = frandom()*pi*2; p.m_Rotspeed = (frandom()-0.5f) * pi; p.m_Gravity = 800.0f; p.m_Friction = 0.8f; vec3 c = BloodColor * (0.75f + frandom()*0.25f); p.m_Color = vec4(c.r, c.g, c.b, 0.75f); m_pClient->m_pParticles->Add(CParticles::GROUP_GENERAL, &p); } }
/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return NULL; IP ip; ip.i = 0; m->net = new_networking(ip, PORT); if (m->net == NULL) { free(m); return NULL; } m->net_crypto = new_net_crypto(m->net); if (m->net_crypto == NULL) { kill_networking(m->net); free(m); return NULL; } m->dht = new_DHT(m->net_crypto); if (m->dht == NULL) { kill_net_crypto(m->net_crypto); kill_networking(m->net); free(m); return NULL; } new_keys(m->net_crypto); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); friendreq_init(&(m->fr), m->net_crypto); LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); init_cryptopackets(m->dht); return m; }
void rain_setup(int tick,int cx,int cy,int cz) { int n; rain_draw_type *rain_draw; rain_draw=view.rain_draws; for (n=0;n!=max_rain_density;n++) { rain_setup_single(rain_draw,cx,cy,cz); rain_draw++; } rain_last_tick=tick; rain_slant_add=random_int(map.rain.slant_add); rain_slant_ang_y=random_float(360); rain_setup_next_slant(tick); }
int main(void) { int i,j; rcc_setup(); gpio_setup(); rng_setup(); while(1){ uint32_t rnd; rnd = random_int(); for(i=0;i!=32;++i){ if ( (rnd & (1 << i))!=0 ) gpio_set(GPIOD, GPIO12); else gpio_clear(GPIOD, GPIO12); /* Delay */ for(j=0;j!=5000000;++j) __asm__("nop"); } } }
Node* generate_tree(std::mt19937& random, int levels, bool is_ghost) { if (levels <= 0) return nullptr; if (levels == 1) { return generate_terminal_node(random, is_ghost); } else { Node* parent = generate_parent_node(random); // Make first child have levels - 1 levels in order to gaurantee `levels` levels parent->children.emplace_back(generate_tree(random, levels - 1, is_ghost)); // Make other children have random between 1, and levels - 1 levels for (int child_index = 1; child_index < parent->GetMinChildren(); ++child_index) { parent->children.emplace_back(generate_tree(random, random_int(random, 1, levels - 1), is_ghost)); } return parent; } }
/** constructor @return an object or a null pointer if the object couldn't be created */ struct Client *Client(void) { struct Client *client; if(!(client = malloc(sizeof(struct Client)))) { perror("Client constructor"); Client_(&client); return 0; } client->is_running= 0; client->id = unique++; client->ms_idle = time_between_prints; client->pages_min = min_page; client->pages_max = max_page; client->prints = random_int(min_prints, max_prints); random_name(client->name); fprintf(stderr, "Client: new, %s (%d) #%p.\n", client->name, client->id, (void *)client); /*fixme! post(empty);*/ return client; }
static u8 choose_prn(void) { u32 total_score = 0; gps_time_t t = get_current_time(); for (u8 prn=0; prn<32; prn++) { if ((acq_prn_param[prn].state != ACQ_PRN_ACQUIRING) || acq_prn_param[prn].masked) continue; acq_prn_param[prn].score[ACQ_HINT_WARMSTART] = manage_warm_start(prn, t, &acq_prn_param[prn].dopp_hint_low, &acq_prn_param[prn].dopp_hint_high); for (enum acq_hint hint = 0; hint < ACQ_HINT_NUM; hint++) { total_score += acq_prn_param[prn].score[hint]; } } u32 pick = random_int() % total_score; for (u8 prn=0; prn<32; prn++) { if ((acq_prn_param[prn].state != ACQ_PRN_ACQUIRING) || acq_prn_param[prn].masked) continue; u32 sat_score = 0; for (enum acq_hint hint = 0; hint < ACQ_HINT_NUM; hint++) sat_score += acq_prn_param[prn].score[hint]; if (pick < sat_score) { return prn; } else { pick -= sat_score; } } log_error("Failed to pick a sat for acquisition!"); return -1; }
//---------------------------------------------------------------------- void Group::distribute_total(const Vector &beta, double sigma){ if (unit_data_.size() <= 1){ unit_data_[0]->set_y(f(total_value_)); return; } if(fabs(sum(unit_values_) - total_value_) > .01){ report_error("TODO(stevescott): need descriptive error here"); } beta_ = & beta; sigma_ = sigma; for(int i = 0; i < unit_data_.size(); ++i){ // Draw j uniformly from the remaining indicies not equal to i. int j = random_int(0, unit_data_.size() - 2); if (j >= i) ++j; modify_unit_value(i, j); unit_data_[i]->set_y(f(unit_values_[i])); unit_data_[j]->set_y(f(unit_values_[j])); } }
JSValueRef js_event_start_wait_random_func(JSContextRef cx,JSObjectRef func,JSObjectRef j_obj,size_t argc,const JSValueRef argv[],JSValueRef *exception) { int min,max,tick,script_idx; char err_str[256]; if (!script_check_param_count(cx,func,argc,3,exception)) return(script_null_to_value(cx)); if (!script_check_fail_in_construct(cx,func,j_obj,exception)) return(script_null_to_value(cx)); script_idx=(int)JSObjectGetPrivate(j_obj); min=script_value_to_int(cx,argv[0]); max=script_value_to_int(cx,argv[1]); tick=random_int(abs(max-min))+min; if (!timers_add(script_idx,tick,script_value_to_int(cx,argv[2]),NULL,timer_mode_single,err_str)) { *exception=script_create_exception(cx,err_str); return(script_bool_to_value(cx,FALSE)); } return(script_bool_to_value(cx,TRUE)); }