C++ Boost Random














































C++ Boost Random




Description :

The library Boost.Random provides numerous random number generators that allow you
to decide how random numbers should be generated.
    Random numbers are required in a number of different problem domains, such as
  • numerics (simulation)
  • games (non-deterministic enemy behavior)
  • security (key generation)
  • testing (random coverage in white-box tests)
1. Generators

     This library provides several pseudo-random number generators. The quality crucially
depends on both the algorithm and its parameters.
   1. Pseudo-random numbers with boost::random::mt19937 :
        The random numbers generated by boost::random::mt19937 are integers.
   2. Real random numbers with boost::random::random_device :
        It is a non-deterministic random number generator, which is a random number
generator that can generate real random numbers.

2. Distributions

  Distributions are Boost.Random classes that map the range of random numbers from a
random number generator to another range.
   1. Random numbers from any min to any max range uniform_int_distribution :
        This distribution lets you define the range of random numbers.The lower
and upper limits  of distributions are inclusive.
   2. The random numbers 0 and 1 with bernoulli_distribution :
        It is a distribution that returns one of two possible results 0 and 1.
    3. binomial_distribution :
              It counts the outcomes of repeated Bernoulli experiments .

          There are many distributions in Boost.Random geometric_distribution,     
    negative_binomial_distribution,Poisson Distribution, discrete_distribution,
normal_distribution,etc ...

Header File Used :
  •   #include<boost/random.hpp>
Advantage Of this code :

    There is no algorithm that needs to be initialized.Thus,predicting the random numbers is impossible.
  Non-deterministic random number generators are often used in security-related applications.

Program :

// For Pseudo-random numbers

#include <boost/random.hpp>
#include<bits/stdc++.h>

int main()
{
std::time_t now = std::time(0);
boost::random::mt19937 gen{static_cast<std::uint16_t>(now)};
std::cout << gen() << '\n';
}

// With Bernoulli_distribution

#include <boost/random.hpp>
#include<bits/stdc++.h>

int main()
{
std::time_t now = std::time(0);
boost::random::mt19937 gen{static_cast<std::uint16_t>(now)};
boost::random::bernoulli_distribution<> cpp;
std::cout << cpp(gen) << '\n';
}


// With uniform_int_distribution

#include <boost/random.hpp>
#include<bits/stdc++.h>

int main()
{
std::time_t now = std::time(0);
boost::random::mt19937 gen{static_cast<std::uint16_t>(now)};
boost::random::uniform_int_distribution<> cpp{100, 300};
std::cout << cpp(gen) << '\n';
}


Output :

// For Pseudo-random numbers

// With Bernoulli_distribution

// With uniform_int_distribution

// Thank you and happy Coding.

Comments