Discrete distributions is a lot easier to do in C++11 with the random header and using std::discrete_distribution. This is example:
#include <iostream>
#include <map>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::discrete_distribution<> d({20,30,40,10});
std::map<int, int> m;
for(int n=0; n<10000; ++n) {
++m[d(gen)];
}
for(auto p : m) {
std::cout << p.first << " generated " << p.second << " times
";
}
}
and this is a sample of the output:
0 generated 2003 times
1 generated 3014 times
2 generated 4021 times
3 generated 962 times
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…