Alım-satım robotlarını ücretsiz olarak nasıl indirebileceğinizi izleyin
Bizi Twitter üzerinde bulun!
Fan sayfamıza katılın
Komut dosyasını ilginç mi buldunuz?
Öyleyse bir link gönderin -
başkalarının da faydalanmasını sağlayın
Komut dosyasını beğendiniz mi? MetaTrader 5 terminalinde deneyin
Kütüphaneler

xoshiro256 Random Number Generator - MetaTrader 5 için kütüphane

Görüntülemeler:
6840
Derecelendirme:
(22)
Yayınlandı:
Güncellendi:
\MQL5\Scripts\Xoshiro256\
MQL5 Freelance Bu koda dayalı bir robota veya göstergeye mi ihtiyacınız var? Freelance üzerinden sipariş edin Freelance'e git

Xoshiro256** Random Number Generator

xoshiro256** (named after its operations: XOR, shift, rotate) is a pseudorandom number generator designed by Sebastiano Vigna in collaboration with David Blackman https://prng.di.unimi.it/

This is a 64-bit all-purpose, rock-solid generator. It has excellent (sub-ns) speed, a state size (256 bits) that is large enough for any parallel application, and it passes all known statistical tests.

The xoshiro256** algorithm is not suitable for cryptographic purposes, but is very fast and has excellent statistical properties.

It is presently used in Microsoft's .NET framework and the JavaScript engines of Chrome, Node.js, Firefox, Safari and Microsoft Edge.

The algorithm used here is translated from the https://prng.di.unimi.it/xoshiro256starstar.c reference source code by David Blackman and Sebastiano Vigna.

For MetaTrader programmers and traders, this is highly relevant for:

  • Monte Carlo Simulations: Accurately simulating price movements, option pricing, or other stochastic processes.

  • Backtesting and Strategy Optimization: Generating random data or events to test the robustness of a trading strategy under various market conditions.

  • Indicator and Expert Advisor (EA) Development: Providing a reliable source of randomness for EAs that use randomized logic, such as in genetic algorithms for optimization.

  • Research and Analysis: The RandomDoubleHighRes method is particularly useful for researchers and quantitative analysts who require a higher degree of precision for their simulations and models, ensuring more accurate and reliable results than standard MathRand().


Class Xoshiro256

The class provides an easy interface for random number generation.

//+------------------------------------------------------------------+
//| Class Xoshiro256                                                 |
//| Usage: Provides an implementation of the Xoshiro256** algorithm. |
//+------------------------------------------------------------------+
class Xoshiro256
  {
public:
   // Constructor and destructor
                     Xoshiro256(void);                                   // Constructor (auto-seed)
                     Xoshiro256(ulong seed);                             // Constructor (single seed)
                     Xoshiro256(ulong s0_,ulong s1_,ulong s2_,ulong s3_);// Constructor (custom seed)

   // Public methods for generating random numbers
   int               RandomInteger(void);                                // Random integer [0, INT_MAX]
   int               RandomInteger(const int max);                       // Random integer [0, max)
   int               RandomInteger(const int min,const int max);         // Random integer [min, max)

   long              RandomLong(void);                                   // Random long [0, LONG_MAX]
   long              RandomLong(const long max);                         // Random long [0, max)
   long              RandomLong(const long min,const long max);          // Random long [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)

   double            RandomDoubleHighRes(void);                          // Random double [0.0, 1.0)
   double            RandomDoubleHighRes(const double max);              // Random double [0.0, max)
   double            RandomDoubleHighRes(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)
  };


If your are doing a lot of Monte Carlo simulations, then this xoshiro256** generator will be much faster and more accurate than MQL's MathRand():

//+------------------------------------------------------------------+
//|                                              speed_benchmark.mq5 |
//|                                        Copyright © 2018, Amr Ali |
//|                             https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#include "Xoshiro256.mqh"
//--- the random number generator object
Xoshiro256 prng;

#define ITER (500*1000*1000)  // 500 millions
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- MathRand() benchmark.
   uint t1 = GetTickCount();
   for(int i = 0; i < ITER; i++)
     {
      MathRand();
     }
   uint ticks1 = GetTickCount() - t1;

//--- Xoshiro256 benchmark.
   uint t2 = GetTickCount();
   for(int i = 0; i < ITER; i++)
     {
      prng.RandomInteger();
     }
   uint ticks2 = GetTickCount() - t2;

//--- Display results.
   PrintFormat("Generating %d random integers: ", ITER);
   PrintFormat("MathRand() -> %u millisec", ticks1);
   PrintFormat("Xoshiro256 -> %u millisec", ticks2);
   PrintFormat("Speed-up factor is %.1fx", (double)ticks1/ticks2);
  }
//+------------------------------------------------------------------+

// sample output:
/*
 Generating 500000000 random integers:
 MathRand() -> 968 millisec
 Xoshiro256 -> 422 millisec
 Speed-up factor is 2.3x
*/

    Updates:

    2023.01.06 - v.1.01 :

    • Updated the class to use the more robust 64-bits xoshiro256** random number generator, instead of the 32-bits xoshiro128**.

    2025.08.01 - v.1.02 :

    • Using a SplitMix64 generator to seed the state to avoid correlation on similar seeds, as recommended by the original authors.
    • Added a seedable constructor with a single ulong. The four 64-bit state variables would be generated using SplitMix64 function.
    • Increased performance of all the public methods for generating random numbers.

    2025.08.03 - v.1.03 :

    • Added RandomDoubleHighRes, a new method for generating high-resolution random doubles in the range of [0.0, 1.0). This method is designed to be more precise than RandomDouble, as it can generate all possible double-precision floating-point values within the specified interval, a feature important for scientific simulations and other high-precision applications.
    • Corrected potential infinite loop bugs in RandomDouble and RandomDoubleHighRes methods.
    • Corrected the implementation of RandomBoolean.

    2025.08.03 - v.1.04 :
    • Faster generation of unbiased random integers in a range by using Lemire's fastrange method. This technique replaces the slow modulo reduction with a debiased integer multiplication.


    RSI Bot MT5 RSI Bot MT5

    RSI Bot, send alert buy when RSI<=20, send alert sell when RSI>=80

    Time Segmented Volume (TSV) Time Segmented Volume (TSV)

    Based on the original “Time Segmented Volume (TSV)” developed by Worden Brothers, Inc.

    XP Forex Trade Manager Grid MT5 XP Forex Trade Manager Grid MT5

    Forex Trade Manager Grid MT5 helps you to managing orders and achieve the goal.

    XP Forex Trade Manager MT5 XP Forex Trade Manager MT5

    Forex Trade Manager MT5 simplifies managing open orders in MetaTrader 5.