static void generate_vector_array(vector_t p[]) { data_t (*rand_next[3])(); int dist, i; for (i = 0; i < 3; i++) { dist = fixed_flag ? 2 : rand_func()%3; switch (dist) { case 0: /* [-1, 1) */ min = -1.0; max = 1.0; rand_next[i] = rand_uniform; break; case 1: /* N(0, 1) */ m = 0.0; sigma = 1.0; rand_next[i] = rand_normal; break; default: /* La(0, 1) */ mu = 0.0; b = 1.0; rand_next[i] = rand_laplace; break; } } for (i = 0; i < N; i++) { p[i].x = rand_next[0](); p[i].y = rand_next[1](); p[i].z = rand_next[2](); } }
static void generate_mass_array(data_t a[]) { data_t (*rand_next)(); int dist, i; m = mu = 1.0/(data_t)N; min = m*0.5; max = m*1.5; dist = fixed_flag ? 2 : rand_func()%3; switch (dist) { case 0: /* [1/2N, 3/2N) */ rand_next = rand_uniform; break; case 1: /* N(1/N, 1/10N) */ sigma = m*1e-1; rand_next = rand_normal; break; default: /* La(1/N, 1/10N) */ b = m*1e-1; rand_next = rand_laplace; break; } for (i = 0; i < N; i++) do a[i] = rand_next(); while (a[i] < min || a[i] > max); }
void roll_for_attack (int attacking, int defending, long (*rand_func)(long max), int *attack_loss, int *defend_loss) { int red[3], white[2], i; bzero(&red, 3*sizeof(int)); bzero(&white, 2*sizeof(int)); for(i=0; i<attacking; i++) { red[i] = rand_func(6) + 1; } if (defending > attacking) defending = attacking; for(i=0; i<defending; i++) { white[i] = rand_func(6) + 1; } cmp_and_swap(red, 0, 1, i); cmp_and_swap(red, 0, 2, i); cmp_and_swap(red, 1, 2, i); cmp_and_swap(white, 0, 1, i); *attack_loss = *defend_loss = 0; for (i=0; i < defending; i++) { if (red[i] > white[i]) { (*defend_loss)++; } else { (*attack_loss)++; } } }
//--------------------------------------------------------- int main() { Py_Initialize(); { boost::python::object main_module=boost::python::import("__main__"); boost::python::object main_namespace=main_module.attr("__dict__"); boost::python::exec("print('Hello, world')", main_namespace); } { boost::python::object main_module=boost::python::import("__main__"); boost::python::object main_namespace=main_module.attr("__dict__"); boost::python::exec("import random", main_namespace); boost::python::object rand=boost::python::eval("random.random()", main_namespace); std::cout<<boost::python::extract<double>(rand)<<std::endl; } { boost::python::object main_module=boost::python::import("__main__"); boost::python::object main_namespace=main_module.attr("__dict__"); boost::python::object rand_mod=boost::python::import("random"); boost::python::object rand_func=rand_mod.attr("random"); boost::python::object rand=rand_func(); std::cout<<boost::python::extract<double>(rand)<<std::endl; } { boost::python::object main_module=boost::python::import("__main__"); boost::python::object main_namespace=main_module.attr("__dict__"); // boost::python::object rand_mod=boost::python::import("base"); // boost::python::object rand_func=rand_mod.attr("f"); // boost::python::object rand=rand_func(8, 9); } { try { boost::python::object rand_mod=boost::python::import("fake_module"); } catch(const boost::python::error_already_set& e) { //std::string perror_str=parse_python_exception(); //std::cout<<"Error in Python: "<<perror_str<<std::endl; } } Py_Finalize(); return 0; }
void load_data(data_t m[], vector_t x[], vector_t v[]) { char fname[256], pname[256]; #ifdef USE_RANDOM_DATA random_flag = 1; #endif #ifdef USE_FIXED_DATA fixed_flag = 1; #endif set_seed(); if (!random_flag) { if (!fixed_flag) prob_no = rand_func()%nprobs; sprintf(fname, "%s/%d/prob%d.dat", data_dir, N, prob_no); if (!read_data_file(fname, m, x, v)) random_flag = 1; } if (random_flag) { if (fixed_flag) prob_no = nprobs; generate_mass_array(m); generate_vector_array(x); generate_vector_array(v); } if (pid == 0) { if (!random_flag || fixed_flag) sprintf(pname, "%d", prob_no); else strcpy(pname, "random"); printf("\n*** Simulation parameters ***\n" "Number of particles: %d\n" "Number of time steps: %d\n" "Interval of time steps: %.1g\n" "Data set No.: %s\n", N, T, DT, pname); } }
static __inline data_t rand_regular() { return rand_norm*rand_func(); }
static __inline data_t rand_laplace() { return rand_func()%2 ? mu-b*log_func(1.0-rand_regular()) : mu+b*log_func(1.0-rand_regular()); }