//Initialisierungsfunktion void initialize(double* ortx, double* orty, double* velx, double* vely, int N, double L, double T_0){ int Teilchenzahl = 0;//Variable zur "richtigen" Speicherung der Ortskoordinaten im Array analog zu den Geschwindigkeitskoordinaten for(int n = 0; n <= 3; ++n){ for (int m = 0; m <= 3; ++m){ ortx[Teilchenzahl] = 1.0/8.0 * (1.0 + 2.0*n) * L;//Plätze der x-Koordinate des Ortes orty[Teilchenzahl] = 1.0/8.0 * (1.0 + 2.0*m) * L;//Plätze der y-Koordinate des Ortes Teilchenzahl++; } } double sum_velx_vorher = 0; double sum_vely_vorher = 0; for(int i = 0; i < N; ++i){ velx[i] = rand_gen(-1.0, 1.0);//Ziehe Zufallszahl für die x-Koordinate der Geschwindigkeit sum_velx_vorher += velx[i];//Summiere alle x-Koordinaten der Geschwindigkeit auf vely[i] = rand_gen(-1.0, 1.0);//Ziehe Zufallszahl für die y-Koordinate der Geschwindigkeit sum_vely_vorher += vely[i];//Summiere alle y-Koordinaten der Geschwindigkeit auf } sum_velx_vorher = sum_velx_vorher/N; sum_vely_vorher = sum_vely_vorher/N; std::cout << "sum_velx_vorher: " << sum_velx_vorher << "\tsum_vely_vorher: " << sum_vely_vorher << std::endl; //Test, ob die Schwerpunktsgeschwindigkeit nach der Reskalierung weiterhin 0 ist double sum_velx_nachher = 0; double sum_vely_nachher = 0; for(int i = 0; i < 16; ++i){ velx[i] -= sum_velx_vorher;//Reskalierung, damit die Schwerpunktsgeschwindigkeit 0 ist für die x-Koordinate sum_velx_nachher += velx[i]; vely[i] -= sum_vely_vorher;//Reskalierung, damit die Schwerpunktsgeschwindigkeit 0 ist für die y-Koordinate sum_vely_nachher += vely[i]; } sum_velx_nachher = sum_velx_nachher/N; sum_vely_nachher = sum_vely_nachher/N; std::cout << "sum_velx_nachher: " << sum_velx_nachher << "\tsum_vely_nachher: " << sum_vely_nachher << std::endl; double N_f = (2.0 * N - 2.0);//Anzahl der Freiheitsgrade double T = 0.0;//Temperatur for(int i = 0; i < N; ++i){ T += velx[i]*velx[i] + vely[i]*vely[i]; } T = T/N_f; double alpha = sqrt(T_0/T);//Skalierungsfaktor for(int i = 0; i < N; ++i){//Skaliere Geschwindigkeiten mit Skalierungsfaktor velx[i] = alpha * velx[i]; vely[i] = alpha * vely[i]; } //Test, ob die Schwerpunktsgeschwindigkeit nach der Skalierung weiterhin 0 ist double sum_velx_nachher2 = 0; double sum_vely_nachher2 = 0; for(int i = 0; i < 16; ++i){ sum_velx_nachher2 += velx[i]; sum_vely_nachher2 += vely[i]; } sum_velx_nachher2 = sum_velx_nachher2/N; sum_vely_nachher2 = sum_vely_nachher2/N; std::cout << "sum_velx_nachher2: " << sum_velx_nachher2 << "\tsum_vely_nachher2: " << sum_vely_nachher2 << std::endl; }
unsigned int tournament_selection(const std::vector<grid>& population) { std::uniform_int_distribution<> dis(0, population.size() - 1); unsigned int parent_a = dis(rand_gen()); int parent_a_score = population[parent_a].score(); unsigned int parent_b = dis(rand_gen()); int parent_b_score = population[parent_b].score(); return parent_a_score > parent_b_score ? parent_a : parent_b; }
typename graph_traits<Graph>::edge_descriptor random_edge(Graph& g, RandomNumGen& gen) { if (num_edges(g) > 1) { uniform_int<RandomNumGen> rand_gen(gen, 0, num_edges(g)-1); typename graph_traits<Graph>::edges_size_type n = rand_gen(); typename graph_traits<Graph>::edge_iterator i = edges(g).first; while (n-- > 0) ++i; // std::advance not VC++ portable return *i; } else return *edges(g).first; }
typename graph_traits<Graph>::vertex_descriptor random_vertex(Graph& g, RandomNumGen& gen) { if (num_vertices(g) > 1) { uniform_int<> distrib(0, num_vertices(g)-1); variate_generator<RandomNumGen&, uniform_int<> > rand_gen(gen, distrib); std::size_t n = rand_gen(); typename graph_traits<Graph>::vertex_iterator i = vertices(g).first; while (n-- > 0) ++i; // std::advance not VC++ portable return *i; } else return *vertices(g).first; }
uint32_t* compute_cut(int start, int end) { //printf("%u %u\n", start, end); if ( end == start ) { return rand_gen(seed[end-1], Z[end-1], Z[end]); } if ( end - start == 1 ) { return multiply(rand_gen(seed[start-1], Z[start-1], Z[start]), rand_gen(seed[end-1], Z[end-1], Z[end]), Z[start-1], Z[start], Z[end]); } int cut = s[start][end]; return multiply(compute_cut(start, cut), compute_cut(cut+1, end), Z[start-1], Z[cut], Z[end]); }
double cMathUtil::RandDoubleSeed(double seed) { unsigned int int_seed = *reinterpret_cast<unsigned int*>(&seed); std::default_random_engine rand_gen(int_seed); std::uniform_real_distribution<double> dist; return dist(rand_gen); }
/** * A method that samples uniformly from the given graph without replacement. * This sampling scheme is used as a baseline to measure the performance of * other sampling schemes --- they have to perform at least as well as this. * * \param[in] graph The graph that we are sampling from. * \param[in] num_samples The number of samples to take. * \param[in] rand_seed The seed for the random number generator. * \param[in] sample A vector containing the edges that were sampled. */ static void uniform_sample (const GraphType& graph, const int& num_samples, const int& rand_seed, const bool& symmetrize, std::vector<bool>& samples) { /* The range for the random number generator is [0..num_edges) */ const int lower = 0; const int upper = graph.get_num_edges (); /* Keep a record of all the edges that have been sampled */ samples.resize (upper); for (int i=0; i<upper; ++i) samples [i] = false; /* Generate 'num_samples' random numbers and pick an edge each time */ prng rand_gen (rand_seed); for (int i=0; i<num_samples; ++i) { int current_edge_index; do { current_edge_index = floor (rand_gen.next (lower, upper)); } while (samples [current_edge_index]); samples [current_edge_index] = true; if (symmetrize) { const int reverse_edge_index = graph.get_reverse_target_index (current_edge_index); samples[reverse_edge_index] = true; } } }
typename graph_traits<Graph>::vertex_descriptor random_vertex(Graph& g, RandomNumGen& gen) { if (num_vertices(g) > 1) { #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581)) std::size_t n = std::random( num_vertices(g) ); #else uniform_int<> distrib(0, num_vertices(g)-1); variate_generator<RandomNumGen&, uniform_int<> > rand_gen(gen, distrib); std::size_t n = rand_gen(); #endif typename graph_traits<Graph>::vertex_iterator i = vertices(g).first; return *(boost::next(i, n)); } else return *vertices(g).first; }
int main(const int argc, const char* const argv[]) { if (argc > 3) { std::cerr << err << "\n At most two arguments are allowed " "(the seed for the random-number generator and the number of elements).\n"; return errcode_parameter; } const int seed = (argc == 1) ? default_seed : boost::lexical_cast<int>(argv[1]); const unsigned int N = (argc < 3) ? default_N : boost::lexical_cast<unsigned int>(argv[2]); vec_t V(N); typedef boost::uniform_int<> uniform_distribution_type; uniform_distribution_type uniform_distribution(0,std::numeric_limits<int>::max()); // is this correct??? typedef boost::variate_generator<base_generator_type&, uniform_distribution_type> generator_type; generator_type rand_gen(base_rand_gen, uniform_distribution); typedef boost::random_number_generator<generator_type> RandomNumberGenerator; RandomNumberGenerator rg(rand_gen); set_random(seed); std::cout << "Underlying random sequence:\n"; for (long n = V.size(); n > 1; --n) std::cout << rg(n) << " "; std::cout << "\n\n"; typedef boost::random_number_generator<base_generator_type> RandomNumberGeneratorWOVariate; RandomNumberGeneratorWOVariate rg_wo_variate(base_rand_gen); set_random(seed); std::cout << "Underlying random sequence (boost::random_number_generator using just boost::mt19937):\n"; for (long n = V.size(); n > 1; --n) std::cout << rg_wo_variate(n) << " "; std::cout << "\n\n"; set_random(seed); std::cout << "Underlying random sequence (randn):\n"; for (long n = V.size(); n > 1; --n) std::cout << ::randn(n) << " "; std::cout << "\n\n"; initialise(V); set_random(seed); std::random_shuffle(V.begin(), V.end(), rg); std::cout << "std::random_shuffle:\n"; output(V); initialise(V); set_random(seed); ::random_shuffle_libcpp(V.begin(), V.end(), rg); std::cout << "std::random_shuffle_libcpp:\n"; output(V); initialise(V); set_random(seed); ::random_shuffle(V.begin(), V.end(), rg); std::cout << "::random_shuffle:\n"; output(V); }
void mutation(std::vector<direction>& child_a_actions) { std::uniform_int_distribution<> dis(0, 99); for(unsigned int i = 0; i < child_a_actions.size(); ++i) { if(dis(rand_gen()) < 10) { child_a_actions[i] = rand_action(); } } }
int test_base_slist(const char* param) { struct slist_t* sl = slist_create(); if (!sl) { fprintf(stderr, "slist create fail\n"); return -1; } rand_seed(time(NULL)); int loop = param ? atoi(param) : 32; int data[loop]; for (int i = 0; i < loop; ++ i) { data[i] = (int)(rand_gen()) % loop; int res = slist_push_front(sl, &data[i]); CHECK(sl, res == 0, "slist push front fail"); res = slist_push_back(sl, &data[i]); CHECK(sl, res == 0, "slist push back fail"); } CHECK(sl, slist_size(sl) == 2 * loop, "slist size fail"); for (int i = loop - 1; i >= 0; -- i) { int res = slist_find(sl, &data[i]); CHECK(sl, res == 0, "slist find fail"); void* p = slist_pop_front(sl); CHECK(sl, p == &data[i], "slist pop front fail"); p = slist_pop_back(sl); CHECK(sl, p == &data[i], "slist pop back fail"); res = slist_find(sl, &data[i]); CHECK(sl, res < 0, "slist find fail"); } CHECK(sl, slist_size(sl) == 0, "slist size fail"); slist_release(sl); return 0; }
/** * A method that samples with replacement using an interval tree. The steps * takes are as follows: * (1) From the adjacency list representation of the graph, we create a new * array that lists the running sum of probabilities for each edge. * (2) Generate 'num_samples' random numbers and pick the interval that this * random number lies in. Increment the count of the picked edge. * * \param[in] graph The graph that we are sampling from. * \param[in] probabilities A map of edges and their probabilities. * \param[in] num_samples The number of samples to take. * \param[in] rand_seed The seed for the random number generator. * \param[out] sample_counts An array containing the number of times each * edge was sampled. */ static void sample_edges (const GraphType& graph, const std::vector<double>& probabilities, const int& num_samples, const int& rand_seed, const bool& symmetrize, std::vector<int>& sample_counts) { /* Resize the sample counts to be as large as the number of edges */ sample_counts.resize (probabilities.size(), 0); /* Create an array containing the running sum of probabilities */ std::vector<double> probability_intervals((1+probabilities.size())); probability_intervals [0] = 0.0; for (int i=1; i<probability_intervals.size(); ++i) { probability_intervals[i] = probability_intervals[(i-1)] + probabilities[(i-1)]; } /* Generate 'num_samples' random numbers and pick an edge each time */ prng rand_gen (rand_seed); for (int i=0; i<num_samples; ++i) { const double current_rand = rand_gen.next (0.0, 1.0); const int interval_index = std::lower_bound (probability_intervals.begin(), probability_intervals.end(), current_rand) - probability_intervals.begin() - 1; if (interval_index >= probability_intervals.size()) { fprintf (stderr, "Could not find interval for %lg\n", current_rand); exit (1); } ++(sample_counts [interval_index]); if (symmetrize) { const int reverse_edge_index = graph.get_reverse_target_index (interval_index); ++(sample_counts[reverse_edge_index]); } } }
void rand_bytes(uint8_t *buf, int size) { uint8_t hash[MD_LEN]; int carry, len = (RAND_SIZE - 1)/2; ctx_t *ctx = core_get(); if (sizeof(int) > 2 && size > (1 << 16)) { THROW(ERR_NO_VALID); } /* buf = hash_gen(size) */ rand_gen(buf, size); /* H = hash(03 || V) */ ctx->rand[0] = 0x3; md_map(hash, ctx->rand, 1 + len); /* V = V + H + C + reseed_counter. */ rand_add(ctx->rand + 1, ctx->rand + 1 + len, len); carry = rand_add(ctx->rand + 1 + (len - MD_LEN), hash, MD_LEN); rand_inc(ctx->rand, len - MD_LEN + 1, carry); rand_inc(ctx->rand, len + 1, ctx->counter); ctx->counter = ctx->counter + 1; }
void Best::localSearch( const std::vector<Graph> &graphs, Solution &solution ) { size_t m = solution.alignment.size1(); size_t n = solution.alignment.size2(); // Create reverse alignment mapping std::vector<std::vector<size_t>> map; create_map(graphs, solution, map); // Count number of graphs each edge is covered in edge_count_matrix edges; count_edges(graphs, map, solution, edges); // Build neighbor lists std::vector<neighbor_list> neighbors; create_neighbor_lists(graphs, map, solution, neighbors); // Active index map std::vector<int> active(m, 0); std::random_device rd; std::minstd_rand rand_gen(rd()); int iteration = 0; bool repeat; do { repeat = false; for(size_t g = 0; g < n-1; ++g) { int best_delta = 0; size_t best_i = 0; size_t best_j = 0; #pragma omp parallel { int prv_best_delta = 0; size_t prv_best_i = 0; size_t prv_best_j = 0; #pragma omp for schedule(static, 1) for(size_t i = 0; i < m; ++i) { for(size_t j = i+1; j < m; ++j) { if(active[i] < iteration && active[j] < iteration) continue; int delta = get_delta(i, j, g, neighbors, edges); if(delta > 0) { active[i] = iteration+1; active[j] = iteration+1; } if(delta > prv_best_delta) { prv_best_delta = delta; prv_best_i = i; prv_best_j = j; } } } #pragma omp critical { if(prv_best_delta > best_delta) { best_delta = prv_best_delta; best_i = prv_best_i; best_j = prv_best_j; } } } if(best_delta > 0) { repeat = true; swap(best_i, best_j, g, iteration, neighbors, edges, solution, active); } } iteration++; } while(repeat); // Extract LCS and solution quality finalize(edges, solution); }
int rand_pos() { static std::uniform_int_distribution<> dis(0, grid_size - 1); return dis(rand_gen()); }
direction rand_action() { static std::uniform_int_distribution<> dis(0, action_count - 1); return static_cast<direction>(dis(rand_gen())); }
bool double_initial_value() { static std::uniform_int_distribution<> dis(0, 99); return dis(rand_gen()) < initial_double_value_percent; }
void sort_population_random(std::vector<grid>& population) { std::shuffle(population.begin(), population.end(), rand_gen()); }
static jboolean JNICALL runRand(JNIEnv *env, jclass clasz, jint bytes) { LOGI("%s", __FUNCTION__); return rand_gen(bytes, 0) == 0; }
void one_point_crossover(const std::vector<direction>& parent_a, const std::vector<direction>& parent_b, grid& child_a, grid& child_b) { std::uniform_int_distribution<> dis(0, parent_a.size()); std::vector<direction> child_a_actions; std::vector<direction> child_b_actions; unsigned int split_a = dis(rand_gen()); unsigned int smaller_parent = std::min(parent_a.size(), parent_b.size()); unsigned int larger_parent = std::max(parent_a.size(), parent_b.size()); unsigned int split = std::min(split_a, smaller_parent); for(unsigned int i = 0; i < smaller_parent; ++i) { if(i < split) { child_a_actions.push_back(parent_a.at(i)); child_b_actions.push_back(parent_b.at(i)); } else { child_a_actions.push_back(parent_b.at(i)); child_b_actions.push_back(parent_a.at(i)); } } // TODO: Refactor, doing this now since each // child can be of different lengths // and the split can be in the larger parent for(unsigned int i = smaller_parent; i < larger_parent; ++i) { if(i < split) { if(larger_parent == parent_b.size()) { child_a_actions.push_back(parent_b.at(i)); } else { child_b_actions.push_back(parent_b.at(i)); } } else { if(larger_parent == parent_b.size()) { child_a_actions.push_back(parent_b.at(i)); } else { child_b_actions.push_back(parent_a.at(i)); } } } mutation(child_a_actions); mutation(child_b_actions); child_a.add_actions(child_a_actions); child_b.add_actions(child_b_actions); }
void custom_map(char* mapz) { int i, rez; int br, phase = 1, lastKey = 5; long int random_element_gen = 0; unsigned long int random_pup_gen = 0; unsigned short mm = 0, pp = 0; clock_t start_lvl_time = clock(), pw_shield_start, pw_clock_start, pw_shovel_start; Levels tank_struct; br = 0; HIGH_SCORE=0; lst->first = NULL; lst->curr = NULL; lst->last = NULL; lst->n = 0; Plst->first = NULL; Plst->last = NULL; init_powerups(); create_map(mapz, 2); tank_struct.kind = -1; tank_struct.smart = 0; LVL = 0; //link_levels(); print_border_side_menu(2, x1m, 25, x2m, 3); // bots left. print_bots_left(); // saljes tezinu izabranu u podesavanjima( Easy-0 medium-1 hard-2) LVL(koji je nivo). NOVO! print_border_side_menu(47, x1m + 20, 56, x2m, 4);// high score counter. // changed coordinates-Andrija // sve spusteno za 21 print_border_side_menu(58, x1m, 67, x2m, 4); // pw disclaimer. print_menu_pups(61); alloc_tank(0, tank_struct); attron(COLOR_PAIR(1)); mvaddstr(68,40,"ESC TO PAUSE"); attroff(COLOR_PAIR(1)); while (1){ time_now(); print_high_score(); random_element_gen++; if (random_element_gen == 280000) random_element_gen = 0, rand_gen(); execute_powerups(&pw_shield_start, &pw_clock_start, &pw_shovel_start, &random_pup_gen); should_spawn_bot(&start_lvl_time, &rez, &br, LVL); if (_kbhit()) execute_our_tank(&mm, &pp, &phase, &lastKey, start_lvl_time, br, 0); execute_bots(1); if (lst->n == 1 && br >= botsInLevel[BOT_DIF][LVL]) { create_map("win.map",0); Sleep(2500); clear(); free_tank(lst->first); delete_powerup_list(); demo_mode(); return; } if ((!powerup.life) || (!strcmp(map_name, "gameover.map")) || (!strcmp(map_name, "gohardorgohome.map")) ) { clear(); delete_tank_list(); delete_powerup_list(); demo_mode(); return; } } }