CRandom Class - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources





The standard random number generator in MQL has a rather limited number of possible values, from 0 to 32767 (15 bits available, 2^15 = 32,768 values ).
It is classified as a Linear Congruential Generator (LCG) and is considered a very basic random number generator.
MQL's implementation of mathematical rand() can be found here: https://www.mql5.com/en/docs/predefined/_randomseed
The generator has an easily detectable statistical flaw and fails most statistical tests for randomness Practical Rand test suite.
Therefore, MathRand() is not suitable for large-scale Monte Carlo simulations or situations where high-quality randomness is critical.
PCG is a family of random number generators that are fast, have good statistical performance, and have a small code size.
This class is a wrapper class for a high-quality 32-bit PCG generator. The output range of this RNG is 2^32, which means it can generate 4,294,967,296 possible values .
The Permuted Congruential Generator (PCG) is a pseudo-random number generation algorithm developed in 2014 that applies an output transformation (random shift or rotation) to eliminate short-period problems in random number generation. Low level This is a problem faced by other LCG generators. It achieves excellent statistical performance with small, fast code and small state size.
This class provides a simple interface for random number generation.
//+------------------------------------------------------------------+ //| CRandom class | //|Purpose: Generate random numbers using a 32-bit PCG generator. | //+------------------------------------------------------------------+ class random number { people : //--- Constructor and destructor C random ( blank ); // Constructor (automatic seed) C random ( constant initialization state, constant initialization ); // Constructor (custom seed) //--- Method to generate random numbers Integer Random integer ( blank ); // Random integer [0, 2147483648) integer Random integer ( constant integer maximum); // Random integer [0, max) integer Random integer ( constant integer minutes, constant integer maximum); // Random integer [minimum value, maximum value] Double random double ( blank ); // Random double [0.0, 1.0) Double random double ( constant double maximum); // Random double [0.0, max) Double random double ( constant double minutes, constant double maximum); // Random double [minimum, maximum) boolean random boolean ( blank ); // random true/false (equal probability) boolean random boolean ( constant double probability of true); // random true/false (using prob_true) double random normal( blank ); // random double (normal distribution) double random normal( constant double mu, constant double sigma); // random double (normal distribution) };
The graph was obtained for random even numbers from 0 to 10
And, the number represents a random integer from 0 to 10
The low-order BITMAP also shows a random pattern

Attachment download
📎 pcg_demo.mq5 (5.96 KB)
📎 pcg_histogram.mq5 (12.24 KB)
📎 pcg_random_bitmap.mq5 (2.69 KB)
📎 mathrand_no_bias.mq5 (5.93 KB)
📎 random.mqh (25.26 KB)
Source: MQL5 #25843
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •