Class CRandom – library MetaTrader 5

MathRand() Function

The standard random number generator in MQL has a fairly limited number of possible values from 0 to 32767 (15-bits usable, 2^15 = 32,768 values).

It is classified as linear congruential generator (LCG) which is considered a very basic random number generator.

The implementation of MQL MathRand() can be found here: https://www.mql5.com/en/docs/predefined/_randomseed

This generator has easily detectable statistical flaws which fail most statistical tests of randomness from the PractRand test suite. 

For this reason, MathRand() is not suitable for large-scale Monte Carlo simulations or where high-quality randomness is critical. 

PCG Random Number Generator

PCG is a family of random number generators, which are fast, statistically excellent, and have small code size.

http://www.pcg-random.org

This class is a wrapper class around the high-quality 32-bits PCG generator. This RNG has an output range of 2^32 which means it can generate 4,294,967,296 possible values.

A permuted congruential generator (PCG) is a pseudorandom number generation algorithm developed in 2014 which applies an output transformation (random shift or rotation) to eliminate the short period problem in the low-order bits that other LCG generators suffer from. It achieves excellent statistical performance with small and fast code, and small state size.

  Exp_PChannel_System - EA MetaTrader 5

The class provides an easy interface for random number generation.

//+------------------------------------------------------------------+
//| Class CRandom                                                    |
//| Usage: Random number generation using the 32-bit PCG generator.  |
//+------------------------------------------------------------------+
class CRandom
  {
public:
   //--- Constructor and destructor
                CRandom(void);                                       // Constructor (auto-seed)
                CRandom(const ulong initstate,const ulong initseq);  // Constructor (custom seed)
                                                                     
   //--- Methods for generation of random numbers                    
   int          RandomInteger(void);                                 // Random integer [0,2147483648)
   int          RandomInteger(const int max);                        // Random integer [0,max)
   int          RandomInteger(const int min,const int max);          // Random integer [min,max)
                                                                     
   double       RandomDouble(void);                                  // Random double  [0.0,1.0)
   double       RandomDouble(const double max);                      // Random double  [0.0,max)
   double       RandomDouble(const double min,const double max);     // Random double  [min,max)
                                                                     
   bool         RandomBoolean(void);                                 // Random true/false (equal probability)
   bool         RandomBoolean(const double prob_true);               // Random true/false (with prob_true)
                                                                     
   double       RandomNormal(void);                                  // Random double (normal distribution)
   double       RandomNormal(const double mu,const double sigma);    // Random double (normal distribution)
  };

This GRAPH was obtained for random double numbers from 0 to 10

  Composite RSI - indicator MetaTrader 5

And, that one for random integer numbers from 0 to 10

The BITMAP of the low-order bits shows also a random pattern



    📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks