25 #ifndef H_SWARM_RANDOM
32 double draw_uniform01();
33 double draw_std_normal();
34 double draw_value_from_config(
const swarm::config& cfg,
const std::string& name,
const int bod,
double min,
double max);
36 double draw_uniform01()
37 {
return static_cast<double>(rand())/static_cast<double>(RAND_MAX); }
39 double draw_std_normal()
42 double rn1 = draw_uniform01();
43 double rn2 = draw_uniform01();
44 return sqrt(-2.*std::log(rn1))*cos(2.*M_PI*(rn2));
48 double draw_value_from_config(
const swarm::config& cfg,
const std::string& name,
const int bod,
double min,
double max)
53 s.str(
""); s << name <<
'_' << bod <<
"_min";
54 if(cfg.count(s.str()))
56 double min_val = atof(cfg.at(s.str()).c_str());
57 min = std::max(min,min_val);
59 s.str(
""); s << name <<
'_' << bod <<
"_max";
60 if(cfg.count(s.str()))
62 double max_val = atof(cfg.at(s.str()).c_str());
63 max = std::min(max,max_val);
66 s.str(
""); s << name <<
'_' << bod;
67 if(cfg.count(s.str()))
69 val = atof(cfg.at(s.str()).c_str());
70 if((val<min)||(val>max))
72 std::cerr <<
"# " << s.str() <<
": " << val <<
" ["<< min <<
", " << max <<
"]\n";
77 s.str(
""); s << name <<
'_' << bod <<
"_sigma";
78 if(cfg.count(s.str()))
81 double sigma = atof(cfg.at(s.str()).c_str());
85 val = mean + sigma*draw_std_normal();
86 }
while((val<min)||(val>max));
94 val = min + (max-min)*draw_uniform01();
100 double draw_value_from_config(
const swarm::config& cfg,
const std::string& name,
double min,
double max)
105 s.str(
""); s << name <<
"_min";
106 if(cfg.count(s.str()))
108 double min_val = atof(cfg.at(s.str()).c_str());
109 min = std::max(min,min_val);
111 s.str(
""); s << name <<
"_max";
112 if(cfg.count(s.str()))
114 double max_val = atof(cfg.at(s.str()).c_str());
115 max = std::min(max,max_val);
118 s.str(
""); s << name;
119 if(cfg.count(s.str()))
121 val = atof(cfg.at(s.str()).c_str());
122 if((val<min)||(val>max))
124 std::cerr <<
"# " << s.str() <<
": " << val <<
" ["<< min <<
", " << max <<
"]\n";
129 s.str(
""); s << name <<
"_sigma";
130 if(cfg.count(s.str()))
133 double sigma = atof(cfg.at(s.str()).c_str());
137 val = mean + sigma*draw_std_normal();
138 }
while((val<min)||(val>max));
146 val = min + (max-min)*draw_uniform01();