Skip to main content

Boost.Random

Boost.Random provides a framework of random number engines and distributions that separate the source of randomness from the shape of the output. It is the direct ancestor of C++11's <random> — the API was adopted almost verbatim into the standard. Boost.Random remains useful for its extra engines, extra distributions, and for codebases that need to support pre-C++11 compilers.

The problem it solves

rand() is global state, low quality, and gives you only uniform integers in [0, RAND_MAX]. Real applications need different distributions (normal, Poisson, Bernoulli), reproducible seeding, and independent generator instances. Boost.Random — and later <random> — formalise the separation between engines (raw bits) and distributions (shaped output).

Engines and distributions

An engine produces a stream of uniformly distributed unsigned integers. A distribution maps that stream into a specific shape (uniform real, normal, binomial, etc.).

basic_usage.cpp
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/normal_distribution.hpp>
#include <iostream>

int main() {
boost::random::mt19937 rng(42); // Mersenne Twister, seeded with 42

// Uniform integer in [1, 6]
boost::random::uniform_int_distribution<> die(1, 6);
std::cout << "die roll: " << die(rng) << "\n";

// Normal distribution: mean=0, stddev=1
boost::random::normal_distribution<> gauss(0.0, 1.0);
std::cout << "normal sample: " << gauss(rng) << "\n";
}

Available engines

EngineQualitySpeedState sizeNotes
mt19937excellentfast2.5 KBDefault choice, period 2^19937-1
mt19937_64excellentfast5 KB64-bit variant
ranlux24highslowsmallLuxury level random numbers
lagged_fibonacci607goodvery fast~4.8 KBLong period, large state
taus88acceptablevery fast12 bytesMinimal state, short period
random_devicecrypto-qualityvariesnoneNon-deterministic, OS entropy
random_device for seeding

boost::random::random_device reads from the operating system's entropy pool (/dev/urandom on Linux, CryptGenRandom on Windows). Use it to seed a fast engine like mt19937, not as the engine itself — it is slow and may block.

Common distributions

distributions.cpp
#include <boost/random.hpp>
#include <iostream>

int main() {
boost::random::mt19937 rng(12345);

// Uniform real in [0.0, 1.0)
boost::random::uniform_real_distribution<> uniform(0.0, 1.0);

// Bernoulli: true with probability 0.3
boost::random::bernoulli_distribution<> coin(0.3);

// Poisson: mean = 4.0
boost::random::poisson_distribution<> poisson(4.0);

// Binomial: 10 trials, p = 0.5
boost::random::binomial_distribution<> binom(10, 0.5);

for (int i = 0; i < 5; ++i) {
std::cout << "uniform=" << uniform(rng)
<< " coin=" << coin(rng)
<< " poisson=" << poisson(rng)
<< " binom=" << binom(rng) << "\n";
}
}

Reproducibility

Given the same engine type and seed, the sequence is identical across runs and platforms. This is essential for simulations, testing, and debugging.

reproducible.cpp
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <cassert>

int main() {
boost::random::mt19937 rng1(42);
boost::random::mt19937 rng2(42);

boost::random::uniform_int_distribution<> dist(1, 100);

for (int i = 0; i < 1000; ++i) {
assert(dist(rng1) == dist(rng2)); // always identical
}
}
Do not use for cryptography

Engines like mt19937 are not cryptographically secure — their internal state can be reconstructed from 624 consecutive outputs. For security-sensitive randomness, use boost::random::random_device directly or a dedicated crypto library.

Seeding from random_device

proper_seeding.cpp
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <iostream>

int main() {
boost::random::random_device entropy;
boost::random::mt19937 rng(entropy()); // non-deterministic seed

boost::random::uniform_int_distribution<> dist(1, 1000);
std::cout << dist(rng) << "\n"; // different every run
}

Boost.Random versus std::random

Featureboost::random<random> (C++11)
Core APIengine + distributionidentical design
mt19937yesyes
random_deviceyesyes
Extra engineslagged_fibonacci, taus88, morefewer
Extra distributionsmore variantsstandard set
Serialization of engine stateoperator<< / operator>>same
Header-onlyyesyes (standard)
Pre-C++11 supportyesno
Which to choose

On C++11 and later, <random> is fine for most use cases — the API is the same because it came from Boost. Reach for Boost.Random when you need an engine or distribution not in <random>, or when targeting a pre-C++11 toolchain.

See also