vistb

ball-in-bin-exact-one-ball-simu

Nov 2nd, 2011
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int bin_num;
  11.     int rounds;
  12.     map<int, int> bin_map;
  13.     map<int, int> freq_map;
  14.  
  15.     cout<<"please input number of balls/bins: ";
  16.     cin>>bin_num;
  17.     cout<<"please input number of rounds: ";
  18.     cin>>rounds;
  19.  
  20.     srand(time(0));
  21.     for (int i=0; i<rounds; ++i)
  22.     {
  23.         bin_map.clear();
  24.  
  25.         srand(rand());
  26.         for (int j=0; j<bin_num; ++j)
  27.             bin_map[rand()%bin_num]++;
  28.  
  29.         int count = 0;
  30.         for (map<int, int>::iterator it=bin_map.begin(); it != bin_map.end(); ++it)
  31.             if ((*it).second == 1)
  32.                 count++;
  33.         freq_map[count]++;
  34.     }
  35.  
  36.     for (map<int, int>::iterator it=freq_map.begin(); it != freq_map.end(); ++it)
  37.         cout<<(*it).first<<" bins have one ball: "<<(*it).second*100.0/rounds<<"%"<<endl;
  38.  
  39.     return 0;
  40. }
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment