【发布时间】:2014-03-01 01:18:04
【问题描述】:
我想在 C++ 中为 n 个数字定义一个特定的概率密度函数 (pdf),然后稍后在我的代码中选择其中一些。
我的 pdf 是:P (x) = (1/logn) * f(x)^(-2)
f(x) 有一个确定性数字,在我的代码前面已经为每个 x 确定了。
我更喜欢使用标准库函数,因为我应该在计算机集群中使用我的程序,而使用其他库(例如 boost)更有可能在该集群中产生更多问题。
我找到的初始代码是:
for(int x=1;x<n+1;x++){
// I calculate all f(x) and therefore P(x) here
}
std::default_random_engine generator;
std::discrete_distribution<int> distribution { .. not sure how to use P(x)s here .. };
int prob[n]={};
for (int i=0; i<n; ++i) {
int number = distribution(generator);
++prob[number];
}
非常感谢。
【问题讨论】:
-
那么,可能的数字是 1 到 n 吗?或者这只是一个例子?
-
抱歉耽搁了。是的,他们是 1 到 n。这是我的实际代码
标签: c++ math random distribution probability-density