int main() { rand_init(); int i=0; for(i=0;i<20;i++){ printf("%d\n",rand_n(5)); } }
void tm_rect_generate_nextto( tm_rect_t* r, tm_rect_t* nextto_r, tm_vect_t* sz, tm_vect_t* map_sz ) { tm_rect_t aux_r; int i, d, start_dir = rand_n(N_DIRS); for( i = 0, d = start_dir; i < N_DIRS; i++, d = (d + 1) % N_DIRS ) { if( d == DIR_UP ) tm_rect_init4( &aux_r, nextto_r->v1.x - sz->x + 1, nextto_r->v2.y, nextto_r->v2.x + sz->x - 1, nextto_r->v2.y + sz->y ); if( d == DIR_DOWN ) tm_rect_init4( &aux_r, nextto_r->v1.x - sz->x + 1, nextto_r->v1.y - sz->y, nextto_r->v2.x + sz->x - 1, nextto_r->v1.y ); if( d == DIR_RIGHT ) tm_rect_init4( &aux_r, nextto_r->v2.x, nextto_r->v1.y - sz->y + 1, nextto_r->v2.x + sz->x, nextto_r->v2.y + sz->y - 1 ); if( d == DIR_LEFT ) tm_rect_init4( &aux_r, nextto_r->v1.x - sz->x, nextto_r->v1.y - sz->y + 1, nextto_r->v1.x, nextto_r->v2.y + sz->y - 1 ); if( (int) aux_r.v1.x < 0 ) aux_r.v1.x = 0; if( (int) aux_r.v1.y < 0 ) aux_r.v1.y = 0; if( (int) aux_r.v2.x > map_sz->x ) aux_r.v2.x = map_sz->x; if( (int) aux_r.v2.y > map_sz->y ) aux_r.v2.y = map_sz->y; if( (int) aux_r.v2.x - (int) aux_r.v1.x < (int) sz->x ) continue; if( (int) aux_r.v2.y - (int) aux_r.v1.y < (int) sz->y ) continue; tm_vect_substract( &aux_r.v2, sz, &aux_r.v2 ); tm_vect_init( &r->v1, rand_range((int)aux_r.v1.x, (int)aux_r.v2.x), rand_range((int)aux_r.v1.y, (int)aux_r.v2.y) ); tm_vect_add( &r->v1, sz, &r->v2 ); break; } }
int heads( void ) { return rand_n( 2 ); }
double StochasticFunctionMinimization::StochasticSubgradientMethodMinimize( StochasticFunctionMinimizationProblem& prob, std::vector<double>& x_opt, double conv_tol, unsigned int max_epochs, bool verbose) { unsigned int dim = prob.Dimensions(); std::vector<double> grad(dim, 0.0); // Initialize x std::vector<double> x(dim); prob.ProvideStartingPoint(x); // Random instance generator size_t N = prob.NumberOfElements(); assert(N > 0); boost::mt19937 rgen(static_cast<const boost::uint32_t>(std::time(0))+1); boost::uniform_int<unsigned int> rdestd(0, static_cast<unsigned int>(N-1)); boost::variate_generator<boost::mt19937, boost::uniform_int<unsigned int> > rand_n(rgen, rdestd); // Optimize a given number of epochs boost::timer total_timer; std::vector<double> avg_grad(dim, 0.0); double lambda = 1.0; // (should be lambda=1/C) double avg_obj = 0.0; for (unsigned int epoch = 0; max_epochs == 0 || epoch < max_epochs; ++epoch) { avg_obj = 0.0; std::fill(avg_grad.begin(), avg_grad.end(), 0.0); // Choose epoch-wide step size double alpha = 1.0 / (static_cast<double>(epoch + 1) * lambda); // Optimize by sampling instances for (size_t n = 0; n < N; ++n) { unsigned int id = rand_n(); // Update average objective and averaged gradient of this epoch avg_obj += prob.Eval(id, x, grad); std::transform(grad.begin(), grad.end(), avg_grad.begin(), avg_grad.begin(), std::plus<double>()); // Perform incremental subgradient update for (unsigned int d = 0; d < dim; ++d) x[d] -= alpha * grad[d]; } // Compute mean gradient and estimated objective double avg_grad_norm = 0.0; for (unsigned int d = 0; d < dim; ++d) avg_grad_norm += avg_grad[d]*avg_grad[d]; avg_grad_norm = std::sqrt(avg_grad_norm); // Output statistics if (verbose && (epoch % 20 == 0)) { std::cout << std::endl; std::cout << " iter time avg_obj |avg_grad|" << std::endl; } if (verbose) { std::ios_base::fmtflags original_format = std::cout.flags(); std::streamsize original_prec = std::cout.precision(); // Iteration std::cout << std::setiosflags(std::ios::left) << std::setiosflags(std::ios::adjustfield) << std::setw(6) << epoch << " "; // Total runtime std::cout << std::setiosflags(std::ios::left) << std::resetiosflags(std::ios::scientific) << std::setiosflags(std::ios::fixed) << std::setiosflags(std::ios::adjustfield) << std::setprecision(1) << std::setw(6) << total_timer.elapsed() << "s "; std::cout << std::resetiosflags(std::ios::fixed); // Objective function std::cout << std::setiosflags(std::ios::scientific) << std::setprecision(5) << std::setiosflags(std::ios::left) << std::setiosflags(std::ios::showpos) << std::setw(7) << avg_obj << " "; // Gradient norm std::cout << std::setiosflags(std::ios::scientific) << std::setprecision(2) << std::resetiosflags(std::ios::showpos) << std::setiosflags(std::ios::left) << avg_grad_norm; std::cout << std::endl; std::cout.precision(original_prec); std::cout.flags(original_format); } // Convergence check if (avg_grad_norm < conv_tol) break; } x_opt = x; return (avg_obj); // This is not exact, but stochastic anyway }
void server_thread_generate_moves( server_thread_t* svt, int cycle ) { int i, j; tm_sv_client_t* cl; //change quest_times to have quests on/off int is_quest = ( sv.wl_quest_count && cycle >= sv.quest_times[sv.quest_id] && cycle < sv.quest_times[sv.quest_id] + sv.wl_quest_duration ); for( i = 0; i < sv.n_clients; i++ ) { cl = sv.clients[i]; if( cl->tid != svt->tid ) continue; tm_entity_movable_t* pl = cl->player; rect_t pl_r; rect_init4( &pl_r, pl->r.v1.x, pl->r.v1.y, pl->r.v2.x, pl->r.v2.y ); value_t first_pl_dir = -1; for( j = 0; j < sv.n_multiple_actions; j++ ) { if( !sv.randomized_actions ) cl->m_actions[j][M_ACT_ID] = sv.m_actions[j]; else { int k, aux = rand_n( 100 ); for( k = 0; k < n_actions; k++ ) if( aux < sv.m_actions_ratios[k] ) break; assert( k < n_actions ); cl->m_actions[j][M_ACT_ID] = k; } if( cl->m_actions[j][M_ACT_ID] == AC_MOVE ) { //burceam: so, if I understand this correctly, we give random direction and speed //to players; then a bit below, (if( is_quest && ( (act_index % 2) != 0 ) ) //we generate a quest move, which will supersede the randomly-establised direction. cl->m_actions[j][M_ACT_SPD] = attribute_type_rand( &entity_types[ET_PLAYER]->attr_types[PL_SPEED] ); cl->m_actions[j][M_ACT_DIR] = attribute_type_rand( &entity_types[ET_PLAYER]->attr_types[PL_DIR] ); int act_index = cycle * sv.n_multiple_actions + j; if( is_quest && ( (act_index % 2) == 0 ) ) { cl->m_actions[j][M_ACT_SPD] = cl->m_actions[j][M_ACT_SPD] / 4; if( entity_types[ET_PLAYER]->attr_types[PL_SPEED].min > cl->m_actions[j][M_ACT_SPD] ) cl->m_actions[j][M_ACT_SPD] = entity_types[ET_PLAYER]->attr_types[PL_SPEED].min; } if( is_quest && ( (act_index % 2) != 0 ) ) cl->m_actions[j][M_ACT_DIR] = server_thread_generate_quest_move( &pl_r, pl->ent_id ); if( sv.straight_move ) { if( first_pl_dir == -1 ) first_pl_dir = cl->m_actions[j][M_ACT_DIR]; else cl->m_actions[j][M_ACT_DIR] = first_pl_dir; } vect_t move_v; vect_scale( &dirs[cl->m_actions[j][M_ACT_DIR]], cl->m_actions[j][M_ACT_SPD], &move_v ); vect_add( &pl_r.v1, &move_v, &pl_r.v1 ); vect_add( &pl_r.v2, &move_v, &pl_r.v2 ); // TO DO: change to allow multiple move records in file //server_register_player_move( pl, i ); #ifdef PRINT_MOVES printf( "cl:%3d cycle:%3d j: %d ; ", cl->cl_id, cycle, j ); printf( "pos: %3d,%3d - last_dist %d ; ", (coord_t)pl->r.v1.x, (coord_t)pl->r.v1.y, cl->last_dist ); printf( "act: %5s spd: %d dir: %5s\n", action_names[ cl->m_actions[j][M_ACT_ID] ], cl->m_actions[j][M_ACT_SPD], dir_names[ cl->m_actions[j][M_ACT_DIR] ] ); #endif } } } }