Amokrane Chentir
Amokrane Chentir

Reputation: 30385

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

Upvotes: 3

Views: 887

Answers (7)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158469

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 

Upvotes: 0

M. S. B.
M. S. B.

Reputation: 29391

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.

Upvotes: 2

Russell Newquist
Russell Newquist

Reputation: 2666

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

Upvotes: 1

Parappa
Parappa

Reputation: 7676

Numerical Recipes discusses a few algorithms for random number generators.

Upvotes: 0

Jonatan Lind&#233;n
Jonatan Lind&#233;n

Reputation: 1485

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

Upvotes: 2

Dan Dyer
Dan Dyer

Reputation: 54475

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.

Upvotes: 3

Jason Kleban
Jason Kleban

Reputation: 20778

I got some interesting responses in this related question:

Biased random number sources

Upvotes: 5

Related Questions