Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Class CRandom - library for MetaTrader 5

Views:
4437
Rating:
(32)
Published:
2019.06.16 06:20
Updated:
2022.03.15 22:32
\MQL5\Scripts\Random\
pcg_demo.mq5 (5.96 KB) view
pcg_histogram.mq5 (12.24 KB) view
\MQL5\Include\
Random.mqh (25.26 KB) view
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

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.

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


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


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



    GbpChf GbpChf

    Advisor for trading Gbp/Chf. Algorithm for bar opening prices. Timeframe H1.

    ArrowDrawingIndiEu ArrowDrawingIndiEu

    This indi will draw OBJ_ARROW_BUY/OBJ_ARROW_SELL when find signals on Arrow Indi Buffers.

    Smoothed CCI Smoothed CCI

    Smoothed CCI

    RSI candles - lite ressource RSI candles - lite ressource

    Improved version based on RSI_candles by © mladen 2018 https://www.mql5.com/en/code/20968