Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int main()
- {
- int bin_num;
- int rounds;
- map<int, int> bin_map;
- map<int, int> freq_map;
- cout<<"please input number of balls/bins: ";
- cin>>bin_num;
- cout<<"please input number of rounds: ";
- cin>>rounds;
- srand(time(0));
- for (int i=0; i<rounds; ++i)
- {
- bin_map.clear();
- srand(rand());
- for (int j=0; j<bin_num; ++j)
- bin_map[rand()%bin_num]++;
- int count = 0;
- for (map<int, int>::iterator it=bin_map.begin(); it != bin_map.end(); ++it)
- if ((*it).second == 1)
- count++;
- freq_map[count]++;
- }
- for (map<int, int>::iterator it=freq_map.begin(); it != freq_map.end(); ++it)
- cout<<(*it).first<<" bins have one ball: "<<(*it).second*100.0/rounds<<"%"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment