Skip to content

Latest commit

 

History

History
48 lines (43 loc) · 1.96 KB

random.md

File metadata and controls

48 lines (43 loc) · 1.96 KB

Generating Random Numbers in C++

The header in C++ provides (pseudo-)random number generation (PRNG):

These types are used via their overloaded call operators.

Example: Printing Ten Random Dice Rolls

#include <random>
#include <iostream>
int main() {
  std::random_device dev; // for seeding
  std::default_random_engine gen{dev()};
  std::uniform_int_distribution<int> dis{1, 6};
  for (int i = 0; i < 10; ++i)
    std::cout << dis(gen) << ' ';
}

Possible Output (will be different each time)

1 1 6 5 2 2 5 5 6 2

Common Generators

?inline

Common Distributions

?inline

See Also

<:cppreference:875716540929015908> Pseudo-random number generation
<:stackoverflow:874353689031233606> Generate random numbers using C++11 random library
<:stackoverflow:874353689031233606> Why is the use of rand() considered bad?