C++11 Random Number Generation

In C++ programs, before the emergence of the new standard, C and C++ relied on a C library function rand to generate random numbers. However, this function generates uniformly distributed pseudorandom numbers, each of which ranges between 0 and a maximum RAND_MAX (at least 32767).

There are some problems in_rand function: in practical programming, many programs need random numbers with different ranges. Some applications require random floating-point numbers. Some programs require nonuniformly distributed random numbers. In order to generate random numbers that satisfy certain requirements, Rand usually converts the range, type or distribution of random numbers generated, but this will introduce non-randomness.

The <random> provided by c++11 implements a random number library, which generates random number sequences through random_number_engines, and random_number_distribution generates random numbers subject to a specific probability distribution using the random number engine.

Here is an example of generating random numbers in C++:

// Console Application 1. cpp: Defines the entry point for console applications.
#include "stdafx.h"
#include <iostream>
//#include "Timer.h"
#include <random>


// const int arraySize = 100000;
int main()
{
	std::default_random_engine e;    // Default Random Number Engine
	for (int i = 0; i < 15; i++)
	{
		auto rnd = e();
		std::cout << rnd << std::endl;
		std::cout << typeid(rnd).name() << std::endl;   // Type of return value
	}
	std::cout << e.max() << " | " << e.min() << std::endl;   // Random Number Engine Range
	//system("pause");
    return 0;
}

Random number classes are defined in std namespaces, so they should be declared. The random number engine is a function object, which is why e() is used to generate random numbers. Every time the program runs, it generates the same random number sequence, which is helpful to debug the program to a certain extent, but sometimes we need to generate different random number sequence every time we run. We can change the state of the engine by setting the seeds of the random number engine. When there is no change, we use the default seeds of the random number, which is why the same sequence of random numbers is generated every time. From the results, we can know that the default seed generation random number range is between 1-2147483646.

Random number seeds can be set by e.seed():

// Console Application 1. cpp: Defines the entry point for console applications.
#include "stdafx.h"
#include <iostream>
//#include "Timer.h"
#include <random>


// const int arraySize = 100000;
int main()
{
	std::default_random_engine e;    // Default Random Number Engine
	e.seed(10);    // Setting the seeds for generating random numbers
	for (int i = 0; i < 15; i++)
	{
		auto rnd = e();
		std::cout << rnd << std::endl;
		//Std:: cout < < typeID (rnd). name ()< < std:: endl; // type of return value
	}
	std::cout << e.max() << " | " << e.min() << std::endl;   // Random Number Engine Range
	//system("pause");
    return 0;
}

If you need to change the range of generated random numbers, you need to use random number distribution classes:

// Console Application 1. cpp: Defines the entry point for console applications.
#include "stdafx.h"
#include <iostream>
//#include "Timer.h"
#include <random>


// const int arraySize = 100000;
int main()
{
	std::default_random_engine e;    // Default Random Number Engine
	std::uniform_int_distribution<int> u(0, 10);    // Random Number Distribution Class, Random Number Range [0,10]
	e.seed(10);    // Setting the seeds for generating random numbers
	for (int i = 0; i < 15; i++)
	{
		auto rnd = u(e);
		std::cout << rnd << "  ";
		//Std:: cout < < typeID (rnd). name ()< < std:: endl; // type of return value
	}
	std::cout << std::endl;
	std::cout << e.max() << " | " << e.min() << std::endl;   // Random Number Engine Range
	//system("pause");
    return 0;
}

In practical applications, random numbers between [0,1] are widely used. In C++11, random numbers satisfying the requirements can be generated in the following ways:

// Console Application 1. cpp: Defines the entry point for console applications.
#include "stdafx.h"
#include <iostream>
//#include "Timer.h"
#include <random>


// const int arraySize = 100000;
int main()
{
	std::default_random_engine e;    // Default Random Number Engine
	std::uniform_real_distribution<double> u(0, 1);    // Random Number Distribution Class, Random Number Range [0,10]
	e.seed(10);    // Setting the seeds for generating random numbers
	for (int i = 0; i < 15; i++)
	{
		auto rnd = u(e);
		std::cout << rnd << "  ";
		//Std:: cout < < typeID (rnd). name ()< < std:: endl; // type of return value
	}
	std::cout << std::endl;
	std::cout << e.max() << " | " << e.min() << std::endl;   // Random Number Engine Range
	//system("pause");
    return 0;
}

In addition, the <random> Library of c++11 also provides different random number engines and random number distribution classes, for example, to generate random numbers between 0 and 1 satisfying normal distribution:

// Console Application 1. cpp: Defines the entry point for console applications.
#include "stdafx.h"
#include <iostream>
#include "Timer.h"
#include <random>


// const int arraySize = 100000;
int main()
{
	std::default_random_engine e;    // Default Random Number Engine
	// The Significance of Knowable Parameters of Structural Function of std::normal_distribution
	// explicit normal_distribution(_Ty _Mean0 = 0.0, _Ty _Sigma0 = 1.0)  
	std::normal_distribution<double> u(0, 1);    // Normal distribution 
	e.seed(10);    // Setting the seeds for generating random numbers
	for (int i = 0; i < 15; i++)
	{
		auto rnd = u(e);
		std::cout << rnd << "  ";
		//Std:: cout < < typeID (rnd). name ()< < std:: endl; // type of return value
	}
	std::cout << std::endl;
	std::cout << e.max() << " | " << e.min() << std::endl;   // Random Number Engine Range
	//system("pause");
    return 0;
}

The Random Number Engine and Random Number Distribution Type provided by C++11 are as follows:
http://www.cplusplus.com/reference/random/?kw=random

Random Number Engine:

Random Number Distribution Class:

------------------------------------------------------------------------------------------------------

Keywords: Programming

Added by TechXpert on Sat, 10 Aug 2019 15:07:05 +0300