2009-11-06 111 views
3

好的库我在寻找已知库,能够生成C,C++和Java的非均匀分布的随机数。产生不均匀的伪随机数

感谢

+0

的http://计算器。 com/questions/1534285/non-uniform-random-number-generator-implementation和http://stackoverflow.com/questions/977354/generating-non-uniform-random-numbers等等。这里没有封闭的形式分布的特殊情况:http://stackoverflow.com/questions/423006/how-do-i-generate-points-that-match-a-histogram – dmckee 2009-11-07 01:29:41

回答

2

的GNU科学图书馆(GSL),http://www.gnu.org/software/gsl/,提供了大量的非均匀分布的随机 - 参见手册中的“随机数分布”的第19章。 (统一的随机数发生器在第17章“随机数发生”中)。该实现是在C.

3

对于Java,一种选择是我的Uncommons Maths库。它支持均匀,高斯,二项式,泊松和指数分布。有一个WebStart demo,所以你可以看到它的作用。

2

看一看Alglib's implementations,他们有几种语言来实现一些基本的分布。

+0

这个链接指向算法的几个分布函数的计算,但我没有找到任何算法来计算从分布中抽取的随机数。 – 2009-11-07 06:02:22

+0

是的,的确如此,您必须使用单独的RNG。例如,invpoissondistribution(k,rng.nextDouble())会给你带参数k的泊松变量。 – 2009-11-07 09:44:26

0

Numerical Recipe小号讨论了随机数生成器的几个算法。

+0

其实我已经写了一些随机数发生器的算法:Box-Muller,Rejection method等等。所以我只是在寻找具有声誉的库来生成非均匀分布的强随机数 – 2009-11-07 00:19:05

1

Boost具有相当广泛的选择随机数的产生,再加上通过几个分布来过滤这些的能力。

0

随着C++ 11有很多可用于在random头产生非均匀伪随机数新的选择。下面的代码示例演示了一些可能的非均匀分布可能的:使用正常分布,你会看到

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <map> 
#include <random> 

int main() 
{ 
    std::random_device rd; 

    std::mt19937 e2(rd()); 

    // 
    // Distribtuions 
    // 
    std::normal_distribution<> dist(2, 2); 
    //std::student_t_distribution<> dist(5); 
    //std::poisson_distribution<> dist(2); 
    //std::extreme_value_distribution<> dist(0,2); 
    //std::lognormal_distribution<> dist(1.6, 0.25); 
    //std::exponential_distribution<> dist(1); 

    std::map<int, int> hist; 
    for (int n = 0; n < 10000; ++n) { 
     ++hist[std::round(dist(e2))]; 
    } 

    for (auto p : hist) { 
     std::cout << std::fixed << std::setprecision(1) << std::setw(2) 
        << p.first << ' ' << std::string(p.second/200, '*') << '\n'; 
    } 
} 

输出与此类似:

-5 
-4 
-3 
-2 * 
-1 *** 
0 ****** 
1 ******** 
2 ********* 
3 ******** 
4 ****** 
5 *** 
6 * 
7 
8 
9