开发者

Good libraries for generating non uniform pseudo-random numbers

I'm looking for known libraries that are able to generate non uniformly distributed random numbers for C, C++开发者_开发百科 and Java.

Thanks


I got some interesting responses in this related question:

Biased random number sources


For Java, one option is my Uncommons Maths library. It supports Uniform, Gaussian, Binomial, Poisson and Exponential distributions. There is a WebStart demo so you can see what it does.


The GNU Scientific Library (GSL), http://www.gnu.org/software/gsl/, provides numerous non-uniform random distributions -- see Chapter 19 of the Manual, "Random Number Distributions". (Uniform random number generators are in Chapter 17, "Random Number Generation"). The implementation is in C.


Have a look at Alglib's implementations, they have a few basic distributions implemented in several languages.


Boost has a fairly wide selection of random number generates, plus the ability to filter these through several distributions.


Numerical Recipes discusses a few algorithms for random number generators.


With C++11 there are a lot of new options available for generating non-uniform Pseudo-random numbers in the random header. The sample code below demonstrates some of the possible non-uniform distributions possible:

#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';
    }
}

using the normal distribution you would see output similar to this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜