我正在使用boost/random.hpp在间隔[1,3]上用随机数填充std :: map,并且将某种东西放在一起,这会使我得到每个计数相对于总数的百分比生成的数字的数量,但正在寻找一个可能更有效的方式来做到这一点。我一直在试图在助推库中找到某些东西,但无法找到完全相关的东西;有没有什么可以使用我的地图(我不想改变我的地图类型)并计算%或其他我应该考虑的东西?有没有更有效的方法来计算百分比?
int main()
{
std::map <int, long> results;
int current;
long one = 0;
long two = 0;
long three = 0;
long total = 0;
boost::random::mt19937 rng;
rng.seed(static_cast<boost::uint32_t> (std::time(0)));
boost::random::uniform_int_distribution<int> random(1,3);
for (int n = 0; n < 1000000; ++n)
{
current = random(rng);
++total;
switch (current)
{
case 1:
++one;
break;
case 2:
++two;
break;
case 3:
++three;
break;
}
}
results[1] = one;
results[2] = two;
results[3] = three;
std::cout << (double) results[1]/total*100 << std::endl; // etc.
}
编辑:我不想以任何方式更改地图容器。
http://codereview.stackexchange.com – 2012-02-12 14:36:33