# Modern C++ idoms and recipes

meshell

18.3K views

## Pseudo-random number generation

The random number library provides classes that generate random and pseudo-random numbers. The library contains two types of components:

*Engines*, which are generators of random numbers (both pseudo-random number generators, which generate integer sequences with a uniform distribution, and true random number generators if available)*Distributions*which convert the output of random number engines into various statistical distributions

#### Random number engines

All engines (except the `random_device`

) produce integer numbers in a uniform distribution using seed data as entropy source.

#### Random number distributions

Random number distributions post-processe the output of an random number engine in such a way that the resulting output is distributed according to a defined statistical probability density function.

### Example

The follwing example prints the uniform distribution of an random generator.

#### TODO

Play around with different engines and distributions.

play around with different engines and distributions.

1

2

3

4

5

6

7

8

9

10

#include <functional>

#include <map>

#include <algorithm>

#include <iostream>

#include <random>

#include <iomanip>

// Taken from "Modern C++ Programming Cookbook" by Marius Bancila

void generate_and_print(std::function<int()> rnd_generator,

const int iterations = 10000) {

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Create your playground on Tech.io

This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.

Suggested playgrounds

Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!

JOIN US ON DISCORD Online Participants