Learn Random in C++ using these simple snippets.

Snippet 1: Random

C++ Random Examples.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(Random1)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(Random1 Random1.cpp)

Radom1.cpp

#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>

using namespace std;

// The main entry point for the application.
auto main() -> int {
  vector<uint8_t> bytes1(100);
  vector<uint8_t> bytes2(100);
  random_device rand1;
  random_device rand2;
  uniform_int_distribution<int32_t> rand_byte_distribution(0, numeric_limits<uint8_t>::max());

  for_each(bytes1.begin(), bytes1.end(), [&](uint8_t& value) {value = static_cast<int8_t>(rand_byte_distribution(rand1));});
  for_each(bytes2.begin(), bytes2.end(), [&](uint8_t& value) {value = static_cast<uint8_t>(rand_byte_distribution(rand2));});

  cout << "First Series:" << endl;
  for (size_t i = 0; i < bytes1.size(); i++) {
    cout << setw(5) << static_cast<int>(bytes1[i]);
    if ((i + 1) % 10 == 0)
      cout << endl;
  }

  cout << endl;
  cout << "Second Series:" << endl;
  for (size_t i = 0; i < bytes2.size(); i++) {
    cout << setw(5) << static_cast<int>(bytes2[i]);
    if ((i + 1) % 10 == 0)
      cout << endl;
  }
}

This code produces the following output:

 First Series:
   217   71  212  132   89    6  110   38   66  243
   194  101   65  242  144  233   88   34  126  224
   180  157  242  209   82  125   37  178   70  190
   232   60  199  132   40  138  212   98  232  242
   178  160    0   51  139  180    6   71  136   58
   136  128   48  128  153    2   76  243   75  237
    72  189   68  167   57  214  223   64  126  193
   160  232  143  183  252  254   55  202  147  154
   107  162   83   56  218   81   47  210  252   41
    44  221  230  243  213  253  130  114  155   62

 Second Series:
   201  133  105    1  176   19  127  172   15   15
   189  125  218   62  149  221  239  199   14  214
    75  250   26   54    0    1   53  146  102   50
    19   68  128  148   34   30   83  234  162  112
   248   17   41    8    4  148  215   24  108   49
   195   81  207  160  220  152   84  216  239   31
    51   56   56   94  248  125  119  158   67  198
    23   45    0  247  217   92   94   91  198  130
   228  199  136  196   51   18  145   46   59  186
    83  143   70   30  129  138   58  237   73   82

Snippet 2: Second Random Example

A second C++ Random example.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(Random2)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(Random2 Random2.cpp)

Random2.cpp

#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>

using namespace std;

// The main entry point for the application.
auto main() -> int {
  // Instantiate random number generator using system-supplied value as seed.
  random_device rand;

  // Generate and display 5 random byte (integer) values.
  vector<uint8_t> bytes(5);
  for_each(bytes.begin(), bytes.end(), [&](uint8_t& value) {value = static_cast<uint8_t>(uniform_int_distribution<int32_t> {0, numeric_limits<uint8_t>::max()}(rand));});

  cout << "Five random byte values:" << endl;
  for (uint8_t byteValue : bytes)
    cout << setw(5) << static_cast<int>(byteValue);
  cout << endl;

  // Generate and display 5 random integers.
  cout << "Five random integer values:" << endl;
  for (int ctr = 0; ctr <= 4; ctr++)
  cout << setw(15) << uniform_int_distribution<int32_t>{numeric_limits<int32_t>::min()}(rand);
  cout << endl;

  // Generate and display 5 random integers between 0 and 100.//
  cout << "Five random integers between 0 and 100:" << endl;
  for (int ctr = 0; ctr <= 4; ctr++)
    cout << setw(10) << uniform_int_distribution<int32_t> {0, 100}(rand);
  cout << endl;

  // Generate and display 5 random integers from 50 to 100.
  cout << "Five random integers between 50 and 100:" << endl;
  for (int ctr = 0; ctr <= 4; ctr++)
    cout << setw(10) << uniform_int_distribution<int32_t> {50, 100}(rand);
  cout << endl;

  // Generate and display 5 random floating point values from 0 to 1.
  cout << "Five Doubles:" << endl;
  for (int ctr = 0; ctr <= 4; ctr++)
    cout << setw(10) << uniform_real_distribution<double>  {}(rand);
  cout << endl;

  // Generate and display 5 random floating point values from 0 to 5.
  cout << "Five Doubles between 0 and 5" << endl;
  for (int ctr = 0; ctr <= 4; ctr++)
    cout << setw(10) << uniform_real_distribution<double> {0, 5}(rand);
  cout << endl;
}

This code produces the following output:

   Five random byte values:
      83  150  213  251  143
   Five random integer values:
         309514098    -1675642838    -1722923713    -1018113968      354020448
   Five random integers between 0 and 100:
            9        49        94        36        33
   Five random integers between 50 and 100:
           57        53        83        60        76
   Five Doubles:
    0.0239899 0.0247499   0.22295  0.124891 0.0632575
   Five Doubles between 0 and 5
       3.8619  0.520773    2.5082   4.53484   2.65078

Snippet 3: C++ Random Example

The third C++ Random example.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(Random3)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(Random3 Random3.cpp)

Random3.cpp

#include <iostream>
#include <random>
#include <string>
#include <vector>

using namespace std;

// The main entry point for the application.
auto main() -> int {
  random_device rand;
  vector<string> male_pet_names = {"Rufus", "Bear", "Dakota", "Fido", "Vanya", "Samuel", "Koani", "Volodya", "Prince", "Yiska"};
  vector<string> female_pet_names = {"Maggie", "Penny", "Saya", "Princess", "Abby", "Laila", "Sadie", "Olivia", "Starlight", "Talla"};

  // Generate random indexes for pet names.
  size_t m_index = uniform_int_distribution<size_t> {0, male_pet_names.size() - 1}(rand);
  size_t f_index = uniform_int_distribution<size_t> {0, female_pet_names.size() - 1}(rand);

  // Display the result.
  cout << "Suggested pet name of the day: " << endl;
  cout << "   For a male:     " << male_pet_names[m_index] << endl;
  cout << "   For a female:   " << female_pet_names[f_index] << endl;
}

This code produces the following output:

 Suggested pet name of the day:
    For a male:     Fido
    For a female:   Princess