나는 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 –