C++C++11C++14C++17C++20Introduction to C++IteratorsLearn C++Syntax

What Are The Differences Between std::rand() And std::mt19937 In Modern C++?

Random numbers are widely used in today’s modern applications. In C we use rand(), srand() and in C++ it is std::rand(), std::srand(). Since C++11, we can use the Mersenne Twister random generators;  mt19937 (std::mt19937) for 32-bit applications and mt19937_64 (std::mt19937_64) for 64-bit applications. Modern C++ allows us to use both old and new random generators. In this post, we explain what are the differences between rand(), std::rand(), std::mt19937(), std::mt19937_64().

What is a random number and rand() in C and C++?

A random number is a number that is randomly chosen in a given range. It is statistically impossible to predict future values based on past or present values and they are uniformly distributed over a defined interval or set.

The rand() is a C function defined in <stdlib.h> and std::rand() is a C++ function defined in the <cstdlib> header. Both are the same and are used to generate pseudo-random numbers in the range of 0 to RAND_MAX. Here is the definition in the <cstdlib> header.

In C we use rand()srand() and in C++ we use std::rand()std::srand() they use a Linear Congruential Generator. While they are added to <cstdlib> to make modern C++ compatible, there a more modern random number generator algorithms, such as Mersenne Twister algorithm.

How can we use rand() in C?

Here is a simple C example to generate a random number between 0 to 100,

How can we use std::rand() in C++?

Here is a simple C++ example to generate a random number between 0 to 100,

What is Mersenne Twister Algorithm in programming?

A Mersenne prime is a prime number used in mathematics that is a number of the form Mn = 2n − 1 where the n is an integer. The Mersenne Twister is a pseudorandom number generator where the period length is chosen to be a Mersenne Prime. It was developed by Makoto Matsumoto in 1997.

Since C++11, the Mersenne Twister mathematical number generator is implemented as a random generator number, it is defined in <random> header as a std::mersenne_twister_engine that is a random number engine based on Mersenne Twister algorithm.

What is std::mt19937 random number generator in modern C++?

In modern C++, there are more useful and modern random number generators. These are std::mt19937 and std::mt19937_64. The std::mt19937 is a 32-bit Mersenne Twister by Matsumoto and Nishimura in 1998, and std::mt19937_64 is a 64-bit Mersenne Twister by Matsumoto and Nishimura in 2000.

The std::mt19937 is a random number generator defined in <random> header in C++17 standard and beyond, producing 32-bit pseudo-random numbers by using the Mersenne Twister algorithm with a state size of 19937 bits. This is why it is called mt19937 and there is a 64-bit version called mt19937_64. Both are defined as an instantiation of the mersenne_twister_engine. Now let’s see their definitions.

Since C++11, mt19937 is defined as below,

Since C++11, mt19937_64 is defined as below,

Is there a simple example to use std::mt19937 in modern C++?

Here is a simple example to use std::mt19937,

What are the differences between std::rand() and std::mt19937 in modern C++?

As you see we define both rand and mt19937 above. Now let’s list their differences between them.

randstd::randstd::mt19937std::mt19937_64
Generator TypeLinear CongruentialLinear CongruentialMersenne Twister Mersenne Twister
Defined Headerstdlib.hcstdlibrandomrandom
Used InC and C++C++C++11C++11
Bits16-bit16-bit or 32-bit32-bit64-bit
Minimum Number0000
Maximum Number3276732767 or
2147483647
41236599959981545732273789042
Min, Max Number Definition0
RAND_MAX
0
RAND_MAX
.min()
.max()
.min()
.max()
Seeding New Generatorsrand()std::srand().seed().seed()
Modern or Classicclassic Cmodern C++modern C++modern C++
Return typeintintuint_fast32_tuint_fast64_t
Recommended in C++?NoNoYesYes
Random PeriodShorterShorterLongerMuch longer
RandomnessLowLowHighHigh
Quality of the Random SequenceNo guaranteeNo guaranteeGoodGood
Using with Iterators and ContainersNoHard to use and SlowEasy and FastEasy and Useful in 64bit
SafetyNot safeNot safeSafeSafe
CodingEasyhard to remember it’s header namehard to remember its name cuz of 19937hard to remember its name cuz of 19937_64

For more details about this feature in C++11 standard, please see these papers; p0205r1

What Are The Differences Between stdrand And stdmt19937 In Modern C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?