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()
.
Table of Contents
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.
1 2 3 |
using _CSTD rand; |
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,
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> #include <stdlib.h> int main () { printf("Random Number:%d\n", rand()%100); getchar(); return(0); } |
How can we use std::rand() in C++?
Here is a simple C++ example to generate a random number between 0 to 100,
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <cstdlib> int main() { std::cout << "Random Number:%d\n", std::rand()%100); system("pause"); return(0); } |
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,
1 2 3 4 5 6 7 8 |
typedef mersenne_twister_engine<unsigned int, 32, 624, 397, 31, 0x9908b0df, 11, 0xffffffff, 7, 0x9d2c5680, 15, 0xefc60000, 18, 1812433253> mt19937; |
Since C++11, mt19937_64
is defined as below,
1 2 3 4 5 6 7 8 |
typedef mersenne_twister_engine<_ULonglong, 64, 312, 156, 31, 0xb5026f5aa96619e9ULL, 29, 0x5555555555555555ULL, 17, 0x71d67fffeda60000ULL, 37, 0xfff7eee000000000ULL, 43, 6364136223846793005ULL> mt19937_64; |
Is there a simple example to use std::mt19937 in modern C++?
Here is a simple example to use std::mt19937
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <random> #include <ctime> int main() { std::mt19937 rnd( std::time(nullptr) ); std::cout << "32bit Random MT Number:" << rnd() << std::endl; system("pause"); return 0; } |
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.
rand | std::rand | std::mt19937 | std::mt19937_64 | |
Generator Type | Linear Congruential | Linear Congruential | Mersenne Twister | Mersenne Twister |
Defined Header | stdlib.h | cstdlib | random | random |
Used In | C and C++ | C++ | C++11 | C++11 |
Bits | 16-bit | 16-bit or 32-bit | 32-bit | 64-bit |
Minimum Number | 0 | 0 | 0 | 0 |
Maximum Number | 32767 | 32767 or 2147483647 | 4123659995 | 9981545732273789042 |
Min, Max Number Definition | 0 RAND_MAX | 0 RAND_MAX | .min() .max() | .min() .max() |
Seeding New Generator | srand() | std::srand() | .seed() | .seed() |
Modern or Classic | classic C | modern C++ | modern C++ | modern C++ |
Return type | int | int | uint_fast32_t | uint_fast64_t |
Recommended in C++? | No | No | Yes | Yes |
Random Period | Shorter | Shorter | Longer | Much longer |
Randomness | Low | Low | High | High |
Quality of the Random Sequence | No guarantee | No guarantee | Good | Good |
Using with Iterators and Containers | No | Hard to use and Slow | Easy and Fast | Easy and Useful in 64bit |
Safety | Not safe | Not safe | Safe | Safe |
Coding | Easy | hard to remember it’s header name | hard to remember its name cuz of 19937 | hard to remember its name cuz of 19937_64 |
For more details about this feature in C++11 standard, please see these papers; p0205r1
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition